Self-study Android notes -- Case Study of Returning data in the Activity (Equipment Selection), androidactivity

Source: Internet
Author: User

Self-study Android notes -- Case Study of Returning data in the Activity (Equipment Selection), androidactivity
1. Create a program:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". main "android: orientation =" vertical "android: gravity =" center "> <ImageView android: id =" @ + id/pet_imgv "android: layout_width =" wrap_content "android: layout_height = "wrap_content" android: layout_gra Principal = "center_horizontal" android: layout_marginBottom = "5dp" android: layout_marginTop = "30dp" android: src = "@ drawable/baby"/> <TextView android: id = "@ + id/pet_dialog_ TV" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "weight" android: layout_marginBottom = "25dp" android: gravity = "center" android: text = ", buy equipment for your baby"/> <TableLayout android: layout _ Width = "fill_parent" android: layout_height = "wrap_content" android: layout_gravity = "center" android: layout_marginBottom = "20dp"> <TableRow android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <TextView android: layout_width = "0dip" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "life:" android: textColor = "@ android: color/black" android: textSize = "14sp"/> <Pro GressBar android: id = "@ + id/progressBar1" style = "? Android: attr/progressBarStyleHorizontal "android: layout_width =" 0dip "android: layout_height =" wrap_content "android: layout_gravity =" center "android: layout_weight =" 2 "/> <TextView android: id = "@ + id/TV _life_progress" android: layout_width = "0dip" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "0" android: gravity = "center" android: textColor = "#000000"/> </TableRow> <TableRow andro Id: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_marginTop = "5dp"> <TextView android: layout_width = "0dip" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "attack force:" android: textColor = "@ android: color/black" android: textSize = "14sp"/> <ProgressBar android: id = "@ + id/progressBar2" style = "? Android: attr/progressBarStyleHorizontal "android: layout_width =" 0dip "android: layout_height =" wrap_content "android: layout_weight =" 2 "/> <TextView android: id = "@ + id/TV _attack_progress" android: layout_width = "0dip" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "0" android: gravity = "center" android: textColor = "#000000"/> </TableRow> <TableRow android: layout_width = "fill_parent "Android: layout_height =" wrap_content "android: layout_marginTop =" 5dp "> <TextView android: layout_width =" 0dip "android: layout_height =" wrap_content "android: layout_weight = "1" android: text = "min:" android: textColor = "@ android: color/black" android: textSize = "14sp"/> <ProgressBar android: id = "@ + id/progressBar3" style = "? Android: attr/progressBarStyleHorizontal "android: layout_width =" 0dip "android: layout_height =" wrap_content "android: layout_weight =" 2 "/> <TextView android: id = "@ + id/TV _speed_progress" android: layout_width = "0dip" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "0" android: gravity = "center" android: textColor = "#000000"/> </TableRow> </TableLayout> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "wrap_content"> <Button android: id = "@ + id/btn_master" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: plugin = "true" android: drawableRight = "@ android: drawable/ic_menu_add" android: onClick = "click" android: drawablePadding = "3dp" android: text = "The owner buys equipment" android: textSize = "14sp"/> <Button android: id = "@ + id/btn_baby" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: plugin = "true" android: drawablePadding = "3dp" android: drawableRight = "@ android: drawable/ic_menu_add" android: onClick = "click2" android: text = "" android: textSize = "14sp"/> </RelativeLayout> </LinearLayout>

The above layout Code uses the control ProgressBar (progress bar), which is used to display the baby's life value, attack value and sensitivity. ProgressBar is usually used to access the network to display the Loading dialog box and the Progress displayed when downloading files.

2. device creation interface:

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/rl" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "vertical"> <View android: layout_width = "30dp" android: layout_height = "30dp" android: background = "@ android: drawable/ic_menu_info_details" android: layout_centerVertical = "true" android: layout_alignParentLeft = "true"/> <TextView android: id = "@ + id/TV _name" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerVertical = "true" android: layout_marginLeft = "60dp" android: text = "Product Name"/> <LinearLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: orientation = "vertical"> <TextView android: id = "@ + id/TV _life" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "13sp" android: text = "lifecycle"/> <TextView android: id = "@ + id/TV _attack" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "13sp" android: text = "attack force" android: layout_marginTop = "5dp"/> <TextView android: id = "@ + id/TV _speed" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "13sp" android: text = "minjie" android: layout_marginTop = "5dp"/> </LinearLayout> </RelativeLayout>


3. Create an ItemInfo class:

Package select.itcast.cn. huichuan;/*** Created by wanglaoda on 15-8-2. */import java. io. serializable; // implement serialization to facilitate the transmission of public class ItemInfo implements Serializable {private String name; private int acctack; private int life; private int speed; public ItemInfo (String name, int acctack, int life, int speed) {this. name = name; this. acctack = acctack; this. life = life; this. speed = speed;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAcctack () {return acctack;} public void setAcctack (int acctack) {this. acctack = acctack;} public int getLife () {return life;} public void setLife (int life) {this. life = life;} public int getSpeed () {return speed;} public void setSpeed (int speed) {this. speed = speed;} public String toString () {return "[name =" + name + ", acctack =" + acctack + ", life =" + life + ", speed = "+ speed +"] ";}}

In addition to passing basic types, Intent can only pass Serializable or Parcelable data. To facilitate data transmission, let the ItemInfo class implement the Serializable interface.

4. Create ShopActivity:

Package select.itcast.cn. huichuan;/*** Created by wanglaoda on 15-8-2. */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 ShopActivity extends Activity implements OnClickListener {private ItemInfo itemInfo; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_shop); itemInfo = new ItemInfo ("", 100, 20, 20); findViewById (R. id. rl ). setOnClickListener (this); TextView mLifeTV = (TextView) findViewById (R. id. TV _life); TextView mNameTV = (TextView) findViewById (R. id. TV _name); TextView mSpeedTV = (TextView) findViewById (R. id. TV _speed); TextView mAttackTV = (TextView) findViewById (R. id. TV _attack); // The TextView display string. If the int value is input here, no error is reported during compilation and mLifeTV is incorrect during running. setText ("lifecycle +" + itemInfo. getLife (); mNameTV. setText (itemInfo. getName () + ""); mSpeedTV. setText ("agility +" + itemInfo. getSpeed (); mAttackTV. setText ("attack power +" + itemInfo. getAcctack ();} @ Override public void onClick (View v) {// TODO Auto-generated method stub switch (v. getId () {case R. id. rl: Intent intent = new Intent (); intent. putExtra ("equipment", itemInfo); setResult (1, intent); finish (); break ;}}}

The key code in the above Code is the last few lines. From this code, we can see that you do not need to specify the Activity to jump to by using the setResult () method to jump to the Activity. The setReult () method is used to make the current Activity return to its caller. Here, it can be understood to make ShopActivity return to MainActivity.

5. Compile the interface interaction code:

Package select.itcast.cn. huichuan; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. widget. progressBar; import android. widget. textView; public class Main extends Activity {private ProgressBar mProgressBar1; private ProgressBar mProgressBar2; private ProgressBar mProgressBar3; Private TextView mLifeTV; private TextView mAttackTV; private TextView mSpeedTV; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mLifeTV = (TextView) findViewById (R. id. TV _life_progress); mAttackTV = (TextView) findViewById (R. id. TV _attack_progress); mSpeedTV = (TextView) findViewById (R. id. TV _speed_progress); InitProgress (); // initialization progress bar} private void initProgress () {mProgressBar1 = (ProgressBar) findViewById (R. id. progressBar1); mProgressBar2 = (ProgressBar) findViewById (R. id. progressBar2); mProgressBar3 = (ProgressBar) findViewById (R. id. progressBar3); mProgressBar1.setMax (1000); // set the maximum value to 1000 mProgressBar2.setMax (1000); mProgressBar3.setMax (1000 );} // enable the new activity and obtain its return value public void click (View view) {Intent intent = new Intent (this, ShopActivity. class); startActivityForResult (intent, 1); // return the request result. The request code is 1} public void click2 (View view) {Intent intent = new Intent (this, ShopActivity. class); startActivityForResult (intent, 1) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); if (data! = Null) {// judge whether the result code is equal to 1, equal to 1 to add equipment to the master, or 2 to add equipment to the baby if (resultCode = 1) {if (requestCode = 1) {ItemInfo info = (ItemInfo) data. getSerializableExtra ("equipment"); // update the value of ssssbar updateProgress (info) ;}}// update the value of ProgressBar private void updateProgress (ItemInfo info) {int progress1 = mProgressBar1.getProgress (); int progress2 = mProgressBar2.getProgress (); int progress3 = mProgressBar3.getProgress (); mProgressBar1.setProgress (progress1 + info. getLife (); mProgressBar2.setProgress (progress2 + info. getAcctack (); mProgressBar3.setProgress (progress3 + info. getSpeed (); mLifeTV. setText (mProgressBar1.getProgress () + ""); mAttackTV. setText (mProgressBar2.getProgress () + ""); mSpeedTV. setText (mProgressBar3.getProgress () + "") ;}@ 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 );}}

6. configuration of the configuration file:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="select.itcast.cn.huichuan" >    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".Main"            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=".ShopActivity"/>    </application></manifest>



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

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.