亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

初學(xué)Android,使用Bundle在Activity間交換數(shù)據(jù)(

系統(tǒng) 2107 0

在Android中,兩個(gè)Acitivity之間是靠Intent傳遞信息的,因?yàn)镮ntent本來就起到信使的作用,所以用它來傳遞數(shù)據(jù)也顯得順理成章了.

Intent 提供了多個(gè)方法來"攜帶"額外的數(shù)據(jù)

putExtras(Bundle data): 向Intent中放入需要"攜帶"的數(shù)據(jù)

putXxx(String key,Xxx date):向Bundle放入Int,Long等各種類型的數(shù)據(jù)(Xxx指代各種數(shù)據(jù)類型的名稱)

putSerializable(String key,Serializable date):向Bundle中放入一個(gè)可序列化的對象.

當(dāng)然Intent也提供了相應(yīng)的取出"攜帶"數(shù)據(jù)的方法

getXxx(String key):從Bundle取出Int,Long 等各種數(shù)據(jù)類型的數(shù)據(jù).

getSerializable(String Key,Serializable data): 從Bundle取出一個(gè)可序列化的對象.

下面以使用getSerializable為例,定義一個(gè)可序列化的Person類,模擬一個(gè)用戶注冊的過程,通過注冊那個(gè)窗口(Acitivity)傳遞注冊信息到另一個(gè)窗口

下面是定義的一個(gè)DTO類Person用來記錄注冊的信息,注意!要定義成可序列化的類,繼承Serializable

    package WangLi.Activity.Bundle;

import java.io.Serializable;

public class Person implements Serializable {
    private String _Name;
    private String _Passwd;
    private String _Gender;
    public String getName()
    {
    	return _Name;
    }
    public String getPass()
    {
    	return _Passwd;
    }
    public String getGender()
    {
    	return _Gender;
    }
    public Person(String Name,String Passwd,String Gender)
    {
    	this._Name = Name;
    	this._Passwd = Passwd;
    	this._Gender = Gender;
    }
}

  

第一個(gè)Activity界面如圖

初學(xué)Android,使用Bundle在Activity間交換數(shù)據(jù)(八)

填入注冊信息后,點(diǎn)"注冊"后跳到新窗口,顯示剛剛輸入的信息

下面是注冊窗口,界面xml 和代碼

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
       
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="請輸入您的注冊信息"
            android:textSize="20sp"
            />
        
        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="用戶名:"
                android:textSize="16sp"
            />
            
            <EditText
                android:id="@+id/name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="請?zhí)顚懴胱缘馁~號(hào)"
                android:selectAllOnFocus="true"
             />
        </TableRow>

        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="密碼:"
                android:textSize="16sp"
            />
            <EditText
                android:id="@+id/passwd"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:password = "true"
                android:selectAllOnFocus="true"
             />
        </TableRow>

        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="性別"
                android:textSize="16sp"
            />
            <RadioGroup
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
            >
                <RadioButton 
                    android:id="@+id/male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="男"
                    android:textSize="16sp"
                    />
                 <RadioButton 
                    android:id="@+id/female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="女"
                    android:textSize="16sp"
                    />
            </RadioGroup>
        </TableRow>

        <TableRow>
            <Button
                android:id="@+id/bn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text='注冊'
                android:textSize="16sp"
                />
        </TableRow>
    </TableLayout>

</LinearLayout>
  
    package WangLi.Activity.Bundle;

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.EditText;
import android.widget.RadioButton;

public class BundleTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button bn = (Button)findViewById(R.id.bn);
        bn.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        		EditText name = (EditText)findViewById(R.id.name);
        		EditText passwd = (EditText)findViewById(R.id.passwd);
        		RadioButton male = (RadioButton)findViewById(R.id.male);
        		String gender = male.isChecked() ? "男" : "女";
        		Person p = new Person(name.getText().toString(),passwd.getText().toString(),gender);
        		//創(chuàng)建Bundle對象
        		Bundle data = new Bundle();
        		data.putSerializable("person", p);
        		//創(chuàng)建一個(gè)Intent
        		Intent intent = new Intent(BundleTest.this,ResultActivity.class);
        		intent.putExtras(data);
        		//啟動(dòng)intent對應(yīng)的Activity
        		startActivity(intent);
        	}
        });
    }
}
  

下面是第接受信息窗口在接受到注冊信息以后的樣子

初學(xué)Android,使用Bundle在Activity間交換數(shù)據(jù)(八)

第二個(gè)接受信息窗口界面xml 及代碼

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
       
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="請輸入您的注冊信息"
            android:textSize="20sp"
            />
        
        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="用戶名:"
                android:textSize="16sp"
            />
            
            <EditText
                android:id="@+id/name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="請?zhí)顚懴胱缘馁~號(hào)"
                android:selectAllOnFocus="true"
             />
        </TableRow>

        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="密碼:"
                android:textSize="16sp"
            />
            <EditText
                android:id="@+id/passwd"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:password = "true"
                android:selectAllOnFocus="true"
             />
        </TableRow>

        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="性別"
                android:textSize="16sp"
            />
            <RadioGroup
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
            >
                <RadioButton 
                    android:id="@+id/male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="男"
                    android:textSize="16sp"
                    />
                 <RadioButton 
                    android:id="@+id/female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="女"
                    android:textSize="16sp"
                    />
            </RadioGroup>
        </TableRow>

        <TableRow>
            <Button
                android:id="@+id/bn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text='注冊'
                android:textSize="16sp"
                />
        </TableRow>
    </TableLayout>

</LinearLayout>
  

    package WangLi.Activity.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ResultActivity extends Activity {
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
    	 super.onCreate(savedInstanceState);
    	 setContentView(R.layout.result);
    	 TextView name = (TextView)findViewById(R.id.name);
    	 TextView passwd = (TextView)findViewById(R.id.passwd);
    	 TextView gender = (TextView)findViewById(R.id.gender);
    	 //獲取啟動(dòng)該Result的Intent
    	 Intent intent = getIntent();
    	 //獲取該intent所攜帶的數(shù)據(jù)
    	 Bundle data = intent.getExtras();
    	 //從Bundle包中取出數(shù)據(jù)
    	 Person p = (Person)data.getSerializable("person");
    	 name.setText("用戶名:"+p.getName());
    	 passwd.setText("密碼:"+p.getPass());
    	 gender.setText("性別:"+p.getGender());
     }
}
  

當(dāng)然,最后也別忘了把所有Activity都加入AndroidManifest.xml中



初學(xué)Android,使用Bundle在Activity間交換數(shù)據(jù)(八)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 99精品久久久久久久婷婷 | 狠狠操综合网 | 中文字幕第一区 | 国产精品久久久久久久久岛 | 日韩免费高清一级毛片久久 | 日日做夜夜做 | 国产成人香蕉在线视频网站 | 视频一区在线播放 | 国产亚洲片 | 中国明星一级毛片免费 | 手机在线看片国产日韩生活片 | 26uuu精品一区二区 | 欧美精品一区二区三区在线 | 免费看欧美一级特黄a大片 免费看欧美一级特黄a大片一 | 亚洲精品一区二区三区四区 | 亚洲视频国产视频 | 爱爱视频在线免费观看 | 四虎影视网| 国产精品久久久久久久免费大片 | 老子影院午夜伦手机不四虎 | 中日韩欧美在线观看 | 玖玖精品视频在线 | 综合色中色 | 日韩欧美视频 | 97视频在线观看免费视频 | 午夜手机看片 | 高清不卡免费一区二区三区 | 亚洲欧美日韩伦中文 | 欧美xxxx8888视频 | 日本黄色录像 | 婷婷免费在线 | 免费观看黄a一级视频日本 免费观看黄色 | 久草青青在线视频 | 一级毛片一级毛片 | 天天操精品 | 天天操夜夜爽 | 国产在线公开视频 | 久久精品国产国语对白 | 国产99视频在线 | 色婷婷综合网 | 日本精品久久久久中文字幕8 |