android基础学习6——intent实现数据传递

在android开发中,经常要在Activity之间传递数据。Intent可以用来开启Activity,同样也可以用来在Activity之间传递数据。

以用户注册为例来演示

第一个activity布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/regist_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="22dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:paddingRight="5dp"
            android:text="用户名:" />
        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入您的用户名"
            android:textSize="14dp"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/regist_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/regist_username"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:paddingRight="5dp"
            android:text="密 码:" />
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入您的密码"
            android:inputType="textPassword"
            android:textSize="14dp"/>
    </LinearLayout>

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/regist_password"
        android:layout_marginLeft="30dp"
        android:contentDescription="性别"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男">
        </RadioButton>
        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioGroup"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="提交用户信息"
        />
    </RelativeLayout>

界面交互代码

package com.example.lenovo.signup;

import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {
    private RadioButton manRadio;
    private RadioButton womanRadio;
    private EditText et_password;
    private Button btn_send;
    private EditText et_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("填写用户信息");
        setContentView(R.layout.activity_main);
        et_name=(EditText) findViewById(R.id.et_name);
        et_password=(EditText) findViewById(R.id.et_password);
        btn_send=(Button) findViewById(R.id.btn_send);
        manRadio=(RadioButton) findViewById(R.id.radioMale);
        womanRadio=(RadioButton) findViewById(R.id.radioFemale);
        btn_send=(Button) findViewById(R.id.btn_send);
        //点击提交用户信息按钮进行数据传递
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                passDate();
            }
        });
    }//数据传递
    public void passDate(){
        //创建intent对象,启动Activity02
        Intent intent=new Intent(this,Activity02.class);
        //将数据存入Intent对象
        intent.putExtra("name",et_name.getText().toString().trim());
        intent.putExtra("password",et_password.getText().toString().trim());
        String str="";
        if(manRadio.isChecked()){
            str="男";
        }else if(womanRadio.isChecked()){
            str="女";
        }
        intent.putExtra("sex",str);
        startActivity(intent);
    }
}
上述代码中,passDate()方法实现了获取用户输入数据,并且将Intent作为载体进行数据传递。
接收数据activity界面

<?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/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:textSize="20dp" />
    <TextView
        android:id="@+id/tv_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:textSize="20dp"/>
    <TextView
        android:id="@+id/tv_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:textSize="20dp" />
</LinearLayout>
activity02
package com.example.lenovo.signup;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;



/**
 * Created by lenovo on 2017/5/7.
 */

public class Activity02 extends AppCompatActivity {
    private TextView tv_name,tv_password,tv_sex;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity02);
        //获取Intent对象
        Intent intent=getIntent();
        //取出key对应的value值
        String name=intent.getStringExtra("name");
        String password=intent.getStringExtra("password");
        String sex=intent.getStringExtra("sex");
        tv_name=(TextView) findViewById(R.id.tv_name);
        tv_password=(TextView) findViewById(R.id.tv_password);
        tv_sex=(TextView) findViewById(R.id.tv_sex);
        tv_name.setText("用户名:"+name);
        tv_password.setText("密  码:"+password);
        tv_sex.setText("性  别:"+sex);
    }
}

在清单文件中配置activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lenovo.signup">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity02"
            android:label="展示用户信息">
        </activity>
    </application>

</manifest>
需要注意的是,android:label属性是用来指定 显示在标题栏上的名称的,如果Activity设置了该属性,则跳到该Activity页面时标题栏会显示在Activity中配置册名称,否则显示在Appli中配置的名称
这里我在activity02中用了
setTitle("填写用户信息");
书上创建了一个activity02用来模拟系统照相机。这里配置的action和category与系统照相机一致。当开启activity时,系统会找到两个符合条件的activity,因此会弹出一个选择的对话框,让用户选择要打开的页面。但是我的手机直接打开了系统照相机,如果改变action和category,能打开相应的activity,不知道是不是手机的设置问题,这里先放着以后解决了再来编辑。
<activity android:name=".Activity02">
    <intent-filter>
         <category android:name="android.intent.category.DEFAULT" />
     </intent-fiter>
</activity>


接下里运行程序进行测试
输入用户名username密码123456


结果如下



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值