Android代码练习:数据传递
来源:互联网 发布:淘宝大学培训班 编辑:程序博客网 时间:2024/05/21 08:35
技术点:本次练习通过intent的putExtra(key,value)向新界面传输数据,通过intent的getStringExtra(key)在新界面接收数据。在Java代码中通过与控件对应的对象的getText()获取控件的值,通过setText()设置控件的值。
注册界面的布局代码与Android代码练习:用户注册界面大致相同,只做了如下改动:
在显示“请输入您的密码”的那个editview中添加了一个inputType的属性并将该属性的值设为“textPassword”;
将“注册”按钮的显示文本改为“提交用户信息”。
接收数据的界面的布局代码:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/username_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textSize="27sp" android:layout_marginTop="7dp" android:layout_marginLeft="7dp" android:gravity="center"/> <TextView android:id="@+id/password_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" android:textSize="27sp" android:layout_marginTop="5dp" android:layout_marginLeft="7dp" android:layout_below="@id/username_show" android:gravity="center"/> <TextView android:id="@+id/sex_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性 别:" android:textSize="27sp" android:layout_marginTop="5dp" android:layout_marginLeft="7dp" android:layout_below="@id/password_show" android:gravity="center"/></RelativeLayout>
MainActivity.java(主界面交互功能实现):
package com.wwj.userregister;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;public class MainActivity extends AppCompatActivity { private EditText username; private EditText password; private RadioButton male; private RadioButton female; private Button submit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register); username = (EditText) findViewById(R.id.username); password = (EditText) findViewById(R.id.password); male = (RadioButton) findViewById(R.id.radioMale); female = (RadioButton) findViewById(R.id.radioFemale); submit = (Button) findViewById(R.id.register_finish); submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { passData();//点击按钮后将输入数据给显示界面 } }); } public void passData(){ Intent intent = new Intent(this,ShowInfo.class); intent.putExtra("username",username.getText().toString()); intent .putExtra("password",password.getText().toString()); String str = ""; if(male.isChecked()){ str = "男"; } else if(female.isChecked()){ str = "女"; } intent.putExtra("sex",str); startActivity(intent); }}
显示界面接收数据并将数据显示出来:
package com.wwj.userregister;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;public class ShowInfo extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_info); Intent intent = getIntent(); String username = intent.getStringExtra("username"); String password = intent.getStringExtra("password"); String sex = intent.getStringExtra("sex"); TextView username_show = (TextView) findViewById(R.id.username_show); TextView password_show = (TextView) findViewById(R.id.password_show); TextView sex_show = (TextView) findViewById(R.id.sex_show); username_show.setText(username_show.getText().toString()+username); password_show.setText(password_show.getText().toString()+password); sex_show.setText(sex_show.getText().toString()+sex); }}
运行结果:
程序运行说明:在主界面填入数据后,MainActivity通过editview对象获取输入的姓名、密码、性别信息,点击提交按钮后,运行响应函数里的数据传递函数passData(),在passData中创建一个intent对象,该intent对象通过显示意图绑定展示信息的界面,将获取的输入信息通过putExtra()存入intent对象,当通过startActivity(intent)启动绑定的展示界面时,在展示界面里获得启动它的intent对象,由getStringExtra()获得intent对象里保存的输入信息,然后通过setText将信息显示在展示界面上。
补充:上述案例通过putExtra和getStringExtra传递数据,下面写下通过putExtras和getExtras传递数据的代码,实现的功能与上诉运行结果一致。
传递数据:
public void passData(){ Intent intent = new Intent(this,ShowInfo.class); Bundle bundle = new Bundle(); bundle.putString("username",username.getText().toString()); bundle.putString("password",password.getText().toString()); String str = ""; if(male.isChecked()){ str = "男"; } else if(female.isChecked()){ str = "女"; } bundle.putString("sex",str); intent.putExtras(bundle); startActivity(intent);}
接收数据:
Intent intent = getIntent();Bundle bundle = intent.getExtras();String username = bundle.getString("username");String password = bundle.getString("password");String sex = bundle.getString("sex");
备注:学习参考《Android移动应用基础教程》。
- Android代码练习:数据传递
- Android代码练习:数据回传
- android 客户端数据传递之一:Web Service 代码
- android post方式传递参数并获取返回数据代码
- Android代码练习:国际化
- Android MVP 代码练习
- Android - Intent - 传递数据
- Android 数据传递Bundle
- Android Bundle数据传递
- Android - Intent - 传递数据
- android javaScript数据传递
- Android传递ArrayList数据
- Android传递数据
- android-数据传递
- android Application 数据传递
- Android fragment数据传递
- android 之 数据传递
- Android--数据传递
- 怎么在微信上使用预约功能
- android之使用原始资源
- OJ提交错误原因
- 某公司笔试题,逆序对
- vim配置
- Android代码练习:数据传递
- 实验报告2_BMP2YUV
- USB type C管脚(我的笔记)
- 同一个世界 三生梦醒 梦境 攻略
- centos 安装mysql
- 企业梦
- 使用pandas的dataFrame实现的决策树算法
- SQL Server服务器修改排序规则的方法
- MYSQL主从配置以及客户端代码实现读写分离功能