1. Create the 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_gravity= "cen Ter_horizontal "android:layout_marginbottom=" 5DP "android:layout_margintop=" 30DP "android:src=" @drawa Ble/baby "/> <textview android:id=" @+id/pet_dialog_tv "android:layout_width=" Wrap_content "an droid:layout_height= "Wrap_content" android:layout_gravity= "Center_horizontal" android:layout_marginbottom= "2 5DP "android:gravity=" center "android:text=" master, quickly buy the baby equipment "/> <tablelayout android:layout_wi Dth= "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:lay out_weight= "1" android:text= "Health Value:" Android:textcolor= "@android: Color/black" a Ndroid:textsize= "14sp"/> <progressbar 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"/> &L T TextView android:id= "@+id/tv_life_progress" android:layout_width= "0dip" Andro id:layout_height= "Wrap_content "android:layout_weight=" 1 "android:text=" 0 "android:gravity=" center " Android:textcolor= "#000000"/> </TableRow> <tablerow Android:layout_wi Dth= "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:" 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 Androi D:id= "@+id/tv_attack_proGress "android:layout_width=" 0dip "android:layout_height=" Wrap_content "Andro id:layout_weight= "1" android:text= "0" android:gravity= "center" Android:textco Lor= "#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= "Agility:" 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"/> &L t;/tablerow> </TableLayout> <relativelayout android:layout_width= "Match_parent" Android:la yout_height= "Wrap_content" > <button android:id= "@+id/btn_master" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentleft= "true" Android:layout_alignparenttop= "true" android:drawableright= "@android:d rawable/ic_menu_add" Android: onclick= "click" android:drawablepadding= "3DP" android:text= "owner Purchase Equipment" Android:textsize= "14s P "/> <buttOn android:id= "@+id/btn_baby" android:layout_width= "Wrap_content" android:layout_height= " Wrap_content "android:layout_alignparentright=" true "android:layout_alignparenttop=" true " android:drawablepadding= "3DP" android:drawableright= "@android:d rawable/ic_menu_add" Android:onclick = "Click2" android:text= "Baby buy equipment" android:textsize= "14sp"/> </relativelayout></linear Layout>
The above layout code uses the control ProgressBar (progress bar), which is used to show the baby's health, attack value and agility.
ProgressBar is frequently used to access the Network Display Loading dialog box and the progress shown when downloading the file.
2. Create the Device 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 oid:background= "@android:d rawable/ic_menu_info_details" android:layout_centervertical= "true" Android:layout_ Alignparentleft= "true"/> <textview android:id= "@+id/tv_name" android:layout_width= "Wrap_con Tent "android:layout_height=" Wrap_content "android:layout_centervertical=" true "Android:layout_margin left= "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 Oid:textsize= "13SP" android:text= "Health value"/> <textview android:id= "@+id/tv_attack" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "13SP" android:text= "Attack" android:layout_margintop= "5DP"/> <textview android:id= "@ +id/tv_speed "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "an Droid:textsize= "13SP" android:text= "Agile" android:layout_margintop= "5DP"/> </linearlayout ></RelativeLayout>
3. Create the ItemInfo class:
Package select.itcast.cn.huichuan;/** * Created by Wanglaoda on 15-8-2. */import java.io.serializable;//enables serialization to facilitate the delivery of data 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.) {this.life = life; } public int GetSpeed () {return speed; } public void Setspeed (int.) {this.speed = speed; } public String toString () {return ' [name= + name + ', Acctack= "+ Acctack +", life= "+ Life +", speed= "+ Speed +"] "; }}
It is important to note that intent, in addition to passing basic types. Only the serializable or parcelable type of data can be passed, in order to facilitate data transmission, here the ItemInfo class to implement 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 ("Golden Sword", 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); TextView display string, where the incoming int value compilation will not error, execution will be error mlifetv.settext ("Health +" + iteminfo.getlife ()); Mnametv.settext (ItemiNfo.getname () + ""); Mspeedtv.settext ("Agility +" + iteminfo.getspeed ()); Mattacktv.settext ("Attack +" + 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 following lines, which can be seen in this code. Jumping activity using the Setresult () method does not require you to specify the activity to jump. The purpose of the Setreult () method is to have the current activity return to its caller, which can be understood here to get shopactivity back to mainactivity.
5. Write 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 Mpro GRESSBAR1; 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 (); Initialize 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 of Mprogressbar2.setmax (1000); Mprogressbar3.setmax (1000); }//Open new activity and want to get his return value public void Click (View view) {Intent Intent = new Intent (This, Shopactivity.clas s); Startactivityforresult (Intent, 1); Returns 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.onactivityr Esult (Requestcode, ResultCode, data); if (data! = NULL) {//infers whether the result code is equal to 1, equal to 1 for the main person to equip, equal to 2 for the baby to join the outfit if (ResultCode = = 1) {if (re Questcode = = 1) {ItemInfo info= (iteminfo) Data.getserializableextra ("EquIpment "); Update the value of ProgressBar updateprogress (info); }}}}//update ProgressBar value private void UpdateProgress (ItemInfo info) {int progress1 = m Progressbar1.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 Actio n 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 would//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 manifest 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>
backhaul data case in Android note--activity (equipment selection)