Android代码练习:数据传递

技术点:本次练习通过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移动应用基础教程》。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值