• Android笔记——Activity中的数据传递案例(用户注册)


    1.创建程序activity_main:

    <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
                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>
    

    在上述代码中,定义了一个相对布局RelativeLayout,该布局中创建了一个EditText和一个Button按钮,分别用于输入内容和点击“提交用户信息”按钮进行数据传递。

    2.创建接收数据Activity界面activity02:

    <?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">
    
        <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>

    3.编写界面交互代码

    Main:

    package passdata.itcast.cn.zhuce;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    
    
    public class Main extends Activity {
    
        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);
            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 intent=new Intent(this, Activity02.class);
    
            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);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }
    

    在上述代码中,passDate()方法实现了获取用户输入数据,并且将Intent作为载体进行数据传递。

    Activity02:

    package passdata.itcast.cn.zhuce;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    /**
     * Created by wanglaoda on 15-7-27.
     */
    public class Activity02 extends Activity{
    
        private TextView tv_name, tv_password, tv_sex;
    
        protected void onCreate(Bundle savedInstanceState){
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity02);
    
            Intent intent=getIntent();
    
            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);
        }
    }
    

    在上述代码中,第20~32行代码通过getIntent()方法获取到Intent对象,然后通过该对象的getStringExtra()方法获取输入的用户名,并将得到的用户名绑定在TextView控件中进行显示。需要注意的是,getStringExtra(String str)方法传入的参数必须是Main中的intent.putExtra()方法中传入的key,否则会返回null。

    4.清单文件配置:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="passdata.itcast.cn.zhuce" >
    
        <application
            android:allowBackup="true"
            android:label="@string/app_name"
            android:icon="@mipmap/ic_launcher"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".Main"
                android:label="填写用户信息" >
                <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中的配置名称,否则显示在Application中的配置名称




    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    hdu 5918(强行水过去..正解KMP)
    hdu 5914(斐波拉契数列)
    hdu 5912(迭代+gcd)
    bzoj 2819(DFS序+树状数组+博弈+lca)
    BestCoder #88(1001 1002)
    hdu 5468(dfs序+容斥原理)
    hdu 5692(dfs序+线段树,好题)
    dfs序题目练习
    csu 1806 & csu 1742 (simpson公式+最短路)
    LuoGuP3774:[CTSC2017]最长上升子序列
  • 原文地址:https://www.cnblogs.com/wanglaoda/p/4937107.html
Copyright © 2020-2023  润新知