Android layout with parameter return and android Layout
Package com. lxj. lesson2_3ID19; import com. example. lesson2_3_id19.R; import com. lxj. other. ageActivity; import com. lxj. other. heightActivity; import com. lxj. other. sexActivity; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. textView; import android. app. activity; import android. content. intent; public class MainActivity extends Activity implements OnClickListener {private static final int REQUEST_AGE = 1; private static final int REQUEST_HEIGHT = 2; private static final int REQUEST_SEX = 3; user user = new User (); TextView tvAge, tvHeight, tvSex; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView (); registerListener ();} private void registerListener () {tvAge. setOnClickListener (this); tvHeight. setOnClickListener (this); tvSex. setOnClickListener (this);} private void initView () {tvAge = (TextView) findViewById (R. id. TV _age); tvHeight = (TextView) findViewById (R. id. TV _height); tvSex = (TextView) findViewById (R. id. TV _sex) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. TV _age: startActivityForResult (new Intent (this, AgeActivity. class), REQUEST_AGE); break; case R. id. TV _height: startActivityForResult (new Intent (this, HeightActivity. class), REQUEST_HEIGHT); break; case R. id. TV _sex: startActivityForResult (new Intent (this, SexActivity. class), REQUEST_SEX); break ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); // visible for debugging, Log is not needed in the program. e ("TAG", "------------- the program returns from" + requestCode + ""); if (resultCode = RESULT_ OK) {switch (requestCode) {case REQUEST_AGE: String age = data. getStringExtra ("age"); tvAge. setText (age); break; case REQUEST_HEIGHT: String height = data. getStringExtra ("height"); tvHeight. setText (height); break; case REQUEST_SEX: String sex = data. getStringExtra ("sex"); tvSex. setText (sex); break;} else {// use log to debug the program. Log is not required in the code. e ("TAG", "------------- the program does not return any ");}}}
package com.lxj.lesson2_3ID19;public class User { String age; String height; String sex; public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public User(String age, String height, String sex) { super(); this.age = age; this.height = height; this.sex = sex; } public User() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "User [age=" + age + ", height=" + height + ", sex=" + sex + "]"; } }
Package com. lxj. other; import com. example. lesson2_3_id19.R; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. textView; public class AgeActivity extends Activity implements OnClickListener {TextView aga1, age2, age3; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_age); initView (); registerListener ();} private void registerListener () {listener (this); age2.setOnClickListener (this); age3.setOnClickListener (this);} private void initView () {aga1 = (TextView) findViewById (R. id. TV _age_1); age2 = (TextView) findViewById (R. id. TV _age_2); age3 = (TextView) findViewById (R. id. TV _age_3) ;}@ Override public void onClick (View v) {// This v represents the currently clicked View // instanceof: indicates whether the object on the left is an instance of the right type if (v instanceof TextView) {// converts v to TextView type TextView TV = (TextView) v; // step Intent intent = getIntent (); // TV. data in getText () is returned. // The returned value is placed in intent. putExtra ("age", TV. getText (); // setResult (); // request succeeded // set the return code and returned value setResult (RESULT_ OK, intent ); // setResult must be used with finish ();}}}
Package com. lxj. other; import com. example. lesson2_3_id19.R; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. textView; public class HeightActivity extends Activity implements OnClickListener {TextView TV _height_1, TV _height_2, TV _height_3; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // The layout cannot be found. // 1. the layout and Code are not in the same project // 2. android. R setContentView (R. layout. activity_height); TV _height_1 = (TextView) findViewById (R. id. height_1); TV _height_2 = (TextView) findViewById (R. id. height_2); TV _height_3 = (TextView) findViewById (R. id. height_3); // OnClickListener TV _height_1.setOnClickListener (this); listener (this); TV _height_3.setOnClickListener (this);} @ Override public void onClick (View v) {// convert v to TextView TV = (TextView) v; // step Intent intent = getIntent (); intent. putExtra ("height", TV. getText (); setResult (RESULT_ OK, intent); finish ();}}
Package com. lxj. other; import com. example. lesson2_3_id19.R; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. textView; public class SexActivity extends Activity implements OnClickListener {TextView TV _sex_1, TV _sex_2; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_sex); TV _sex_1 = (TextView) findViewById (R. id. sex_1); TV _sex_2 = (TextView) findViewById (R. id. sex_2); // implement OnClickListener TV _sex_1.setOnClickListener (this); TV _sex_2.setOnClickListener (this) ;}@ Override public void onClick (View v) {TextView TV = (TextView) v; intent intent = getIntent (); intent. putExtra ("sex", TV. getText (); // set the return code returned value setResult (RESULT_ OK, intent); finish ();}}
<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: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "Zhang San" android: textSize = "20dp" android: layout_margin = "5dp"/> <TextView android: id = "@ + id/TV _age" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "Age" android: textSize = "18dp" android: layout_margin = "10dp"/> <TextView android: id = "@ + id/TV _height" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "height" android: textSize = "18dp" android: layout_margin = "10dp"/> <TextView android: id = "@ + id/TV _sex" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "gender" android: textSize = "18dp" android: layout_margin = "10dp"/> </LinearLayout>
<? 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 _age_1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_margin = "10dp" android: gravity = "center" android: text = "android: textSize =" 18dp "/> <TextView android: id =" @ + id/TV _age_2 "android: layout_width =" match_parent "android: layout_height = "wrap_content" android: layout_margin = "10dp" android: gravity = "center" android: text = "18-30 years old" android: textSize = "18dp"/> <TextView android: id = "@ + id/TV _age_3" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_margin = "10dp" android: gravity = "center" android: text = "over 30" android: textSize = "18dp"/> </LinearLayout>
<? 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/height_1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "less than 170cm" android: textSize = "18dp" android: layout_margin = "10dp"/> <TextView android: id = "@ + id/height_2" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "170-180 cm" android: textSize = "18dp" android: layout_margin = "10dp"/> <TextView android: id = "@ + id/height_3" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "180cm or above" android: textSize = "18dp" android: layout_margin = "10dp"/> </LinearLayout>
<? 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/sex_1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_margin = "10dp" android: gravity = "center" android: text = "male" android: textSize = "18dp"/> <TextView android: id = "@ + id/sex_2" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_margin = "10dp" android: gravity = "center" android: text = "" android: textSize = "18dp"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lesson2_3_id19" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.lxj.lesson2_3ID19.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.lxj.other.AgeActivity"> </activity> <activity android:name="com.lxj.other.HeightActivity"> </activity> <activity android:name="com.lxj.other.SexActivity"> </activity> </application></manifest>