1. 程式人生 > >Android 建立一個activity 及建立Intent物件跳轉介面並傳值

Android 建立一個activity 及建立Intent物件跳轉介面並傳值

一、建立一個activity

步驟1、建立一個java類,該類必須繼承Android.app.Activity

步驟2、重寫該類的onCreate(Bundle)方法

步驟3、建立一個佈局檔案res/layout

步驟4、在onCreate方法中呼叫setContentView(佈局檔案),繫結佈局

步驟5、在Androidmainifest.xml檔案中註冊新的窗體類

步驟6、進行窗體開發。

二、建立Intent跳轉物件並傳值

1、①new Intent(原介面,目標介面)

Intent intent=new Intent(MainActivity.this,ResultActivity.class);

  ② Intent intent = new Intent();

intent.setClass(原介面,目標介面);

2、Intent物件傳值

intent.putExtra("user",user);

3、跳轉

startActivity(intent);

4、另一個Activity獲取傳入的值

Intent intent=getIntent();

int user=intent.getIntExtra("user", -1);
//int型的 若傳過來的值為空則給賦值-1

三、自定義對話方塊

        <activity
            android:name=".ResultActivity"
            android:label="結果" 
            android:theme="@android:style/Theme.Dialog"

>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>  
            </intent-filter>
        </activity>

MainActivity.java

package com.example.demo3_guss;

import java.util.Random;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {

	private RadioButton rdoJD, rdoST, rdoB;
	private Button btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.rdoJD = (RadioButton) findViewById(R.id.rdoJD);
		this.rdoST = (RadioButton) findViewById(R.id.rdoST);
		this.rdoB = (RadioButton) findViewById(R.id.rdoB);

		this.btn = (Button) findViewById(R.id.btn);

		this.btn.setOnClickListener(new Viewocl());
	}

	private class Viewocl implements View.OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.btn:

				int user = 0;
				if (rdoJD.isChecked()) {
					// Toast.makeText(MainActivity.this,
					// "你選中了剪刀!",Toast.LENGTH_SHORT).show();
					user = 1;
				} else if (rdoST.isChecked()) {
					// Toast.makeText(MainActivity.this,
					// "你選中了石頭!",Toast.LENGTH_SHORT).show();
					user = 2;
				} else {
					// Toast.makeText(MainActivity.this, "你選中了布!",
					// Toast.LENGTH_SHORT).show();
					user = 3;
				}

				
				
				Intent intent=new Intent();
				intent.setClass(MainActivity.this, ResultActivity.class);
				intent.putExtra("user", user);
				startActivity(intent);
			
			
				break;

			default:
				break;
			}

		}
	}

	
}
ResultActivity.java
package com.example.demo3_guss;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ResultActivity extends Activity {
	
	private TextView txtShow;
	private Button btnClose;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);
		
		this.txtShow=(TextView) findViewById(R.id.txtShow);
		this.btnClose=(Button) findViewById(R.id.btnClose);
		
		this.btnClose.setOnClickListener(new Viewocl());
		
		Intent intent = getIntent();

		int user = intent.getIntExtra("user", -1);

		int cpu = new Random().nextInt(3) + 1;

		String message="";
		if (user == 3 && cpu == 1) {
			message="使用者:" + convert(user) + "  VS    " + convert(cpu)
					+ "電腦\n對不起,輸了!";
//			Toast.makeText(
//					ResultActivity.this,
//					"使用者:" + convert(user) + "  VS    " + convert(cpu)
//							+ "電腦\n對不起,輸了!", Toast.LENGTH_SHORT).show();
		} else if (user > cpu) {
			
			message="使用者:" + convert(user) + "  VS    " + convert(cpu)
					+ "電腦\n恭喜你,贏了!";
//			Toast.makeText(
//					ResultActivity.this,
//					"使用者:" + convert(user) + "  VS    " + convert(cpu)
//							+ "電腦\n恭喜你,贏了!", Toast.LENGTH_SHORT).show();
		} else if (user < cpu) {
			message="使用者:" + convert(user) + "  VS    " + convert(cpu)
					+ "電腦\n對不起,輸了!";
//			Toast.makeText(
//					ResultActivity.this,
//					"使用者:" + convert(user) + "  VS    " + convert(cpu)
//							+ "電腦\n對不起,輸了!", Toast.LENGTH_SHORT).show();
		} else if (user == 1 && cpu == 3) {
			message="使用者:" + convert(user) + "  VS    " + convert(cpu)
					+ "電腦\n恭喜你,贏了!";
//			Toast.makeText(
//					ResultActivity.this,
//					"使用者:" + convert(user) + "  VS    " + convert(cpu)
//							+ "電腦\n恭喜你,贏了!", Toast.LENGTH_SHORT).show();
		} else {
			message="使用者:" + convert(user) + "  VS    " + convert(cpu)
					+ "電腦\n平局!";
//			Toast.makeText(
//					ResultActivity.this,
//					"使用者:" + convert(user) + "  VS    " + convert(cpu)
//							+ "電腦\n平局!", Toast.LENGTH_SHORT).show();
		}
		
		this.txtShow.setText(message);

	}

	public String convert(int num) {

		String result = "";

		switch (num) {
		case 1:
			result = "剪刀";
			break;

		case 2:
			result = "石頭";
			break;
		case 3:
			result = "布";
			break;
		default:
			break;
		}

		return result;
	}

	public class Viewocl implements View.OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.btnClose:
				finish();
				break;

			default:
				break;
			}
		}
		
		
	}
}

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="請您出拳" />

    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/rdoJD"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="剪刀"
            android:textSize="18dp" />

        <RadioButton
            android:id="@+id/rdoST"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="石頭"
            android:textSize="18dp" />

        <RadioButton
            android:id="@+id/rdoB"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="布"
            android:textSize="18dp" />
    </RadioGroup>
    
    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="出拳"
        android:textSize="18dp"
        ></Button>

</LinearLayout>

activity_result.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
<TextView 
    android:id="@+id/txtShow"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="16dp"
    />
<Button 
    android:id="@+id/btnClose"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="確定"
    />

</LinearLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo3_guss"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ResultActivity"
            android:label="結果" 
            android:theme="@android:style/Theme.Dialog">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>  
            </intent-filter>
        </activity>
    </application>

</manifest>