欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

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

系統(tǒng) 1908 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í)顚懴胱?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í)顚懴胱?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

您的支持是博主寫作最大的動(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ì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 久久九九综合 | 97超级碰碰碰碰在线视频 | 亚洲自拍偷拍色图 | 免费黄色片网站 | 中日欧洲精品视频在线 | 久草草视频在线观看免费高清 | 欧美日韩在线国产 | 国产美女极品免费视频 | 国产精品一区二区久久 | 狠狠色丁香婷婷综合久久片 | 欧美色专区 | 成人看的一级毛片 | 亚洲1区2区3区4区 | 国产福利视频一区 | 成人福利在线观看 | 99精彩视频| av国产精品 | 好吊妞gao988在线播放 | 国产东北普通话对白 | 久久久久久久国产精品电影 | 欧美黑人性受xxxx喷水 | 日韩第一| 一级黄色片武则天 | 精品一区二区三区自拍图片区 | 国产在线精品一区 | 色成人亚洲 | 国产成人99 | 国产精品久久久久久久久久 | 欧美日韩国产在线播放 | 91青青草视频 | 久久99精品久久久久久秒播 | 女人一级毛片免费视频观看 | 日韩伦理一区 | 国产成人av电影 | 欧美一级片在线播放 | 在线视频一区二区 | 免费国产一区二区三区 | 奇米777四色成人影视 | 国产亚洲精品久久久久久老妇小说 | 国产亚洲精品久久久久久久久动漫 | 污视频在线免费播放 |