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

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

系統(tǒng) 2166 0

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

Intent 提供了多個(gè)方法來(lái)"攜帶"額外的數(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è)可序列化的對(duì)象.

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

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

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

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

下面是定義的一個(gè)DTO類Person用來(lái)記錄注冊(cè)的信息,注意!要定義成可序列化的類,繼承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ù)(八)

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

下面是注冊(cè)窗口,界面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="請(qǐng)輸入您的注冊(cè)信息"
            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="請(qǐng)?zhí)顚?xiě)想注冊(cè)的賬號(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='注冊(cè)'
                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對(duì)象
        		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對(duì)應(yīng)的Activity
        		startActivity(intent);
        	}
        });
    }
}
  

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

初學(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="請(qǐng)輸入您的注冊(cè)信息"
            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="請(qǐng)?zhí)顚?xiě)想注冊(cè)的賬號(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='注冊(cè)'
                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

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 97人人干| 欧美日本一道免费一区三区 | 免费观看日本污污ww网站精选 | 天天色综合三 | 一道本免费视频 | 久久精品国产久精国产80cm | 182午夜在线观看 | 成人国产精品久久久免费 | 亚洲图片综合区 | 欧美日韩中文国产一区二区三区 | 色婷婷精品大视频在线蜜桃视频 | 一 级 黄 中国色 片 | 美女胸又大又黄www网站 | 日本韩国欧美在线 | 九九热这里只有 | 日日夜夜摸摸 | 国产成人精视频在线观看免费 | 久久综合99re88久久爱 | 美女被cao的视频免费看 | 久久久亚洲精品视频 | 色网在线免费观看 | 亚洲欧美乱综合图片区小说区 | 亚洲天天网综合自拍图片专区 | 亚洲精品久久久久综合网 | 亚洲一区二区视频 | 香蕉精品视频在线观看入口 | 欧美影院一区二区 | 亚洲成人综合网站 | 久久久久久国产精品免费 | 欧美精品一区二区精品久久 | 青青青青久久国产片免费精品 | 国产伦精品一区二区三区四区 | 国产激情一区二区三区 | 亚洲精品毛片久久久久久久 | 女人18级毛片久久 | 亚洲综合色婷婷 | 九九久久亚洲综合久久久 | 日本黄色mv | 久久综合久久综合久久综合 | 97av在线视频 | 女孕学护士一级毛片 |