使用Bundle在Activity之間交換資料

never123450發表於2014-07-02

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xwy.xinxi.MainActivity$PlaceholderFragment"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用者名稱:" />
    <EditText 
        android:id="@+id/user"
        android:minWidth="200px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView 
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密碼:" />
    <EditText 
        android:id="@+id/pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="200px"
        android:inputType="textPassword" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="確認密碼:" />

    <EditText
        android:id="@+id/repwd"
         android:minWidth="200px"
        android:inputType="textPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />   
     <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="E-mail地址:" />

    <EditText
        android:id="@+id/email"
         android:minWidth="400px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交" />

</LinearLayout>

MainActivity.java

package com.xwy.xinxi;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	final public int CODE = 0x717;//定義一個請求碼
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);

		Button submit = (Button) findViewById(R.id.submit);
		submit.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String user = ((TextView)findViewById(R.id.user)).getText().toString();
				String pwd = ((EditText)findViewById(R.id.pwd)).getText().toString();
				String repwd=((EditText)findViewById(R.id.repwd)).getText().toString();	//獲取輸入的確認密碼
				String email=((EditText)findViewById(R.id.email)).getText().toString();	//獲取輸入的E-mail地址
				if(!"".equals(user) && !"".equals(pwd) && !"".equals(email)){
					if(!pwd.equals(repwd)){
						Toast.makeText(MainActivity.this, "2次輸入密碼不一樣,請重新輸入", Toast.LENGTH_SHORT).show();
						((EditText)findViewById(R.id.pwd)).setText("");
						((EditText)findViewById(R.id.repwd)).setText("");
						((EditText)findViewById(R.id.pwd)).getResources();//讓密碼框獲取焦點
					}else{//將輸入的資訊儲存到Bundle中並啟動一個新的Activity顯示輸入的使用者註冊資訊
						Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
						Bundle bundle = new Bundle();
						bundle.putCharSequence("user", user);//儲存使用者名稱
						bundle.putCharSequence("pwd", pwd);
						bundle.putCharSequence("email", email);
						intent.putExtras(bundle);//將bundle物件新增到Intent物件中
						startActivity(intent);//啟動新的Activity
					}
				}else{
					Toast.makeText(MainActivity.this, "請將資訊輸入完整", Toast.LENGTH_SHORT).show();
				}
				
			}
		});
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==CODE && resultCode==CODE){
			((EditText)findViewById(R.id.pwd)).setText("");
			((EditText)findViewById(R.id.repwd)).setText("");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}

register.sml

<?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/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10px"
        android:text="使用者名稱:" />
    <TextView 
        android:id="@+id/pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10px"
        android:text="密碼:" />
    <TextView 
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10px"
        android:text="E-main:" />
    <Button 
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回上一步"/>

</LinearLayout>

RegisterActivity.java

package com.xwy.xinxi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class RegisterActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);//設定該activity中要顯示的內容檢視
		final Intent intent = getIntent();//獲取Intent物件
		Bundle bundle = intent.getExtras();//獲取傳遞的資料包
		TextView user = (TextView) findViewById(R.id.user);//獲取顯示使用者名稱的TextView元件
		user.setText("使用者名稱:"+bundle.getString("user"));//獲取輸入的使用者名稱並顯示到TextView元件中
		TextView pwd = (TextView) findViewById(R.id.pwd);
		pwd.setText("密碼:"+bundle.getString("pwd"));
		TextView email = (TextView) findViewById(R.id.email);
		email.setText("E-mail:"+bundle.getString("email"));
		
		Button button = (Button) findViewById(R.id.back);//獲取“返回上一部”按鈕
		button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				setResult(0x717,intent);//設定返回的結果碼,並返回撥用該Activity的Activity
				finish();
			}
		});
	}
}

在Mainfest中配置

<activity
            android:label="顯示使用者註冊資訊"
            android:icon="@drawable/ic_launcher"
            android:name=".RegisterActivity"></activity>

相關文章