Activity参数传递

来源:互联网 发布:管理自己的淘宝店铺 编辑:程序博客网 时间:2024/06/05 16:17

实现两个activity的跳转和参数传递。用intent实现。

第一个activity(MainActivity):

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.exam.exp4_1.MainActivity">    <LinearLayout        android:id="@+id/linearlayout1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <EditText            android:id="@+id/username"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入用户名"            android:textSize="20sp"            android:layout_margin="10dp"            android:singleLine="true"/>        <EditText            android:id="@+id/password"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入密码"            android:textSize="20sp"            android:layout_margin="10dp"            android:singleLine="true" />        <EditText            android:id="@+id/birth"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入出生年份"            android:textSize="20sp"            android:layout_margin="10dp"            android:singleLine="true"/>    </LinearLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_margin="10dp"        android:layout_below="@id/linearlayout1"       >        <Button            android:id="@+id/login"            android:text="@string/login"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="20sp"            android:layout_alignParentLeft="true"            android:layout_marginTop="10dp"            />        <Button            android:id="@+id/register"            android:text="@string/register"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="20sp"            android:layout_alignParentRight="true"            android:layout_marginTop="10dp"            />    </RelativeLayout></LinearLayout>
MainActivity.java:

package com.exam.exp4_1;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import java.util.Calendar;public class MainActivity extends AppCompatActivity {    private Button login;    private Button register;    private EditText username;    private EditText password;    private EditText birth;    private Calendar now;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        login = (Button)findViewById(R.id.login);        register = (Button)findViewById(R.id.register);        username = (EditText)findViewById(R.id.username) ;        password = (EditText)findViewById(R.id.password) ;        birth = (EditText)findViewById(R.id.birth) ;        login.setOnClickListener(new View.OnClickListener(){            public void onClick(View v){                String name = username.getText().toString();                String psd = password.getText().toString();                int birthyear = Integer.parseInt(birth.getText().toString());                now = Calendar.getInstance();//获取现在时间                //将现在时间转换成int类型,减去输入即可                int  nowyear = now.get(Calendar.YEAR) ;                int age = nowyear - birthyear;
                //实现参数传递                Intent it = new Intent(MainActivity.this,B_Activity.class);                Bundle bundle = new Bundle();                bundle.putString("username",name);                bundle.putString("password",psd);                bundle.putInt("birth",age);                it.putExtras(bundle);                startActivity(it);            }        });        register.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setAction("exp2MainActivity");                //intent.addCategory(Intent.Category.DEFAULT);                MainActivity.this.startActivity(intent);            }        });    }}
登录按钮连接的界面(BActivity):

activity_b.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:layout_marginTop="10dp">        <TextView            android:id="@+id/username"            android:text="@string/username"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="15sp"            />        <EditText            android:id="@+id/text1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_toRightOf="@id/username"            android:singleLine="true"            android:hint=""            />    </RelativeLayout>    <RelativeLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="10dp">        <TextView            android:id="@+id/password"            android:text="@string/password"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="15sp"            />        <EditText            android:id="@+id/text2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_toRightOf="@id/password"            android:singleLine="true"            />    </RelativeLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp">        <TextView            android:id="@+id/age"            android:text="@string/age"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="15sp"            />        <EditText            android:id="@+id/text3"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:singleLine="true"            android:layout_toRightOf="@id/age"            />    </RelativeLayout></LinearLayout>
BActivity.java:

package com.exam.exp4_1;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.EditText;public class B_Activity extends AppCompatActivity {    private EditText text1;    private EditText text2;    private EditText text3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_b);        Intent it = getIntent();        Bundle bundle = it.getExtras();        if(bundle != null){            String name = bundle.getString("username");            String psd = bundle.getString("password");            int age = bundle.getInt("birth");            text1 = (EditText)findViewById(R.id.text1);            text1.setText(name+"");            text2 = (EditText)findViewById(R.id.text2);            text2.setText(psd+"");            text3 = (EditText)findViewById(R.id.text3);            text3.setText(age+"");        }    }}
注册按钮连接的界面(前边实现的注册界面):

在前边实现了的注册信息的activity的AndroidManiFest。xml中设置对一个 action 等属性:

<activity android:name="com.exam.exp2.MainActivity">    <intent-filter>        <action android:name="exp2MainActivity"/>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />        <category android:name="android.intent.category.DEFAULT"/>    </intent-filter></activity>
实现效果:





阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机多头数据线 一个月内多头借贷后果 头皮屑多头痒怎么办 股票多头是什么意思 多头和空头是什么意思 空头和多头是什么意思 多头排列与空头排列 头皮屑多头痒 多头排列图解 什么是多头排列 多头螺纹加工方法 多头自动点胶机 多头不死跌势不止 车载多头充电器 多头自动攻丝机 多头行车记录仪 空头 多姆 为什么姓崔的人不多 婀娜多姿 多姿多彩 绰约多姿 阿娜多姿 卓多姿 顾盼多姿 绚丽多姿 多姿 多彩多姿 什么多姿 妩媚多姿 婀娜多姿意思 多姿多彩的意思 多姿多彩意思 江山如此多姿 婀娜多姿拼音 多姿多彩的活动 多姿多彩的近义词 卓多姿连衣裙 卓多姿女装专卖店 婀娜多姿的近义词 江山如此多姿 世清狂