Android 使用bundle在activity之间交换数据 DEMO (填写注册信息实例)

代码打包下载:

http://www.400gb.com/file/26764782


java 代码,MainActivity:

 Button submit=(Button)findViewById(R.id.submit);	//获取提交按钮
 submit.setOnClickListener(new View.OnClickListener() {
	
	@Override
	public void onClick(View v) {
		String user=((EditText)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, "密码不一致", Toast.LENGTH_LONG).show();
				((EditText)findViewById(R.id.pwd)).setText("");	//清空密码编辑框
				((EditText)findViewById(R.id.repwd)).setText("");	//清空确认密码编辑框
				((EditText)findViewById(R.id.pwd)).requestFocus();	//让密码编辑框获得焦点
			}else{	//将输入的信息保存到Bundle中,并启动一个新的Activity显示输入的用户注册信息
				Intent intent=new Intent(MainActivity.this,RegisterActivity.class);
				Bundle bundle=new Bundle();	//创建并实例化一个Bundle对象
				bundle.putCharSequence("user", user);	//保存用户名
				bundle.putCharSequence("pwd", pwd);	//保存密码
				bundle.putCharSequence("email", email);	//保存E-mail地址
				intent.putExtras(bundle);	//将Bundle对象添加到Intent对象中
				startActivity(intent);	//启动新的Activity
			
			}
		}else{
			Toast.makeText(MainActivity.this, "注册信息不全", Toast.LENGTH_LONG).show();
		}

	}
});
   


java代码 RegisterActivity

public class RegisterActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);	//设置该Activity中要显示的内容视图
		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);		//获取显示密码的TextView组件
		pwd.setText("密码:"+bundle.getString("pwd"));		//获取输入的密码并显示到TextView组件中
		TextView email=(TextView)findViewById(R.id.email);		//获取显示E-mail的TextView组件
		email.setText("邮箱:"+bundle.getString("email"));		//获取输入的E-mail并显示到TextView组件中
		
	}

}

xml ,activity_main

<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:orientation="vertical" >

    <EditText
        android:id="@+id/user"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="用户名" />

    <EditText
        android:id="@+id/pwd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/repwd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="确认密码"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="邮箱地址" />

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

</LinearLayout>


xml ,register

<?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="fill_parent"
        android:layout_height="wrap_content"
        android:text="用户名" />

    <TextView
        android:id="@+id/pwd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="密码" />

    <TextView
        android:id="@+id/email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="E-mail地址" />

</LinearLayout>


Manifest:

    <application
        android:icon="@drawable/ic_launcher"
        android:label="注册"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="注册" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".RegisterActivity"
            android:icon="@drawable/ic_launcher"
            android:label="信息" >
        </activity>
    </application>


效果:

 

代码打包下载:

http://www.400gb.com/file/26764782

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值