Self-learning Android notes -- data transfer case in Activity (user registration), androidactivity

Source: Internet
Author: User

Self-learning Android notes -- data transfer case in Activity (user registration), androidactivity
1. Create the program 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 = "username:"/> <EditText android: id = "@ + id/et_name" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "Enter your username" 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 = "password: "/> <EditText android: id =" @ + id/et_password "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: hint = "enter your password" 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 = "gender" android: orientation = "horizontal"> <RadioButton android: id = "@ + id/radioMale" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: checked = "true" android: text = "male"/> <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>

In the above Code, a relative layout RelativeLayout is defined. An EditText and a Button are created in the layout for entering the content and clicking the "Submit user information" Button for data transmission.

2. Create the Activity interface activity02 for receiving data:

<?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. Write interactive code on the Interface

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. activities; 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 privilege. 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 = "male";} else if (womanRadio. isChecked () {str = "female";} 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 );}}

In the above Code, the passDate () method obtains user input data and uses Intent as the carrier for data transmission.

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 ("username:" + name); TV _password.setText ("password:" + password); TV _sex.setText ("sex:" + sex );}}

In the above Code, 20th ~ The 32-line code obtains the Intent object through the getIntent () method, obtains the input username through the getStringExtra () method of the object, and binds the obtained username to the TextView Control for display. Note that the parameter passed in by the getStringExtra (String str) method must be the key passed in the intent. putExtra () method in Main; otherwise, null is returned.

4. configuration of the configuration file:

<? 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 =" enter user information "> <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 =" show user information "> </activity> </application> </manifest>

It should be noted that the android: label attribute is used to specify the name displayed on the title bar. If the Activity sets this attribute, it will jump to the Activity.
The configuration name in the Activity is displayed in the title bar of the page; otherwise, the configuration name in the Application is displayed.



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.