Self-Study The data transfer case in Android note--activity (user Registration)

Source: Internet
Author: User
<span id="Label3"></p><span style="font-size:18px"><span style="font-size:18px">1. Create the program activity_main:</span></span><p><p></p></p><pre name="code" class="html"><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=" @+i D/regist_username "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:l ayout_centerhorizontal= "true" android:layout_marginleft= "10dp" android:layout_marginright= "10dp" Android oid:layout_margintop= "22dp" android:orientation= "horizontal" > <textview Android:layout_widt h= "80dp" android:layout_height= "wrap_content" android:gravity= "right" Android:paddingrigh t= "5dp" android:text= "user name:"/> <edittext android:id= "@+id/et_name" Android:la Yout_width= "match_parent" android:layout_height= "wrap_content" android:hint= "please Enter your username" an Droid:textsize= "14dp"/> </linearlayout> <linearlayout android:id= "@+id/regist_password" android:layout_width= "match_paren T "android:layout_height=" wrap_content "android:layout_below=" @+id/regist_username "android:layout_ce nterhorizontal= "true" android:layout_marginleft= "10dp" android:layout_marginright= "10dp" Android:layo ut_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:layo Ut_width= "match_parent" android:layout_height= "wrap_content" android:hint= "please Enter your password" Andro Id:inputtype= "textpassword" android:textsize= "14dp"/> </LinearLayout> <radiogroup Android Oid: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_cont ENT "android:layout_height=" wrap_content "android:checked=" true "android:text=" male "/> <radiobutton android:id= "@+id/radiofemale" android:layout_width= "wrap_content" an droid:layout_height= "wrap_content" android:text= "female"/> </RadioGroup> <button android:id = "@+id/btn_send" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:lay out_below= "@+id/radiogroup" android:layout_centerhorizontal= "true" android:layout_margintop= "24dp" an droid:text= "submit User information"/></RelativeLayout> </pre><br><p><p><span style="font-size:14px">In the preceding code, a relative layout relativelayout is defined, which creates a edittext and a button buttons, respectively, for entering content and clicking the "submit User information" button for data transfer.</span></p></p><span style="font-size:18px"><span style="font-size:18px">2. Create Receive data activity interface Activity02:</span></span><p><p></p></p><pre name="code" class="html"><?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 "& Gt <textview android:id= "@+id/tv_name" android:layout_width= "wrap_content" android:layout_height= "wra P_content "android:gravity=" Center "android:layout_margintop=" 10dp "android:textsize=" 20DP "/> & Lt TextView android:id= "@+id/tv_password" android:layout_width= "wrap_content" android:layout_height= "wra P_content "android:gravity=" Center "android:layout_margintop=" 10dp "android:textsize=" 20DP "/> & Lt TextView android:id= "@+id/tv_sex" android:layout_width= "wrap_content" android:layout_height= "wrap_con Tent "android:gravity=" Center "android:layout_margintop=" 10dp "android:textsize=" 20DP "/></linea Rlayout></pre><p><p></p></p><span style="font-size:18px"><span style="font-size:18px">3. Writing interface Interaction code</span></span><p><p><span style="font-size:14px">Main:</span><br></p></p><p><p></p></p><pre name="code" class="java">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= "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 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); }}</pre><p><p></p></p><p><p><span style="font-size:14px">In the above code, the Passdate () method implements the acquisition of user input data and transmits the data intent as a carrier.</span><br></p></p><p><p><span style="font-size:14px">Activity02:</span></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">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 ("user name:" +name); Tv_password.settext ("secret code:" +password); Tv_sex.settext ("sex :" +sex);} }</pre></pre><p><p></p></p><p><p><span style="font-size:14px">In the preceding code, the 20th to 32nd line of code obtains the intent object through the Getintent () method and then obtains the input user name through the Object's Getstringextra () method, and binds the resulting user name to be displayed in the TextView control. 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 it will return Null.</span><br></p></p><span style="font-size:18px"><span style="font-size:18px">4. Manifest File configuration:</span></span><p><p></p></p><pre style="background-color:#ffffff; color:#000000; font-family:‘DejaVu Sans Mono‘; font-size:9.0pt"><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><manifest xmlns:android= "/http Schemas.android.com/apk/res/android "package=" passdata.itcast.cn.zhuce "> <application Android:allowbac kup= "true" android:label= "@string/app_name" android:icon= "@mipmap/ic_launcher" android:theme= "@style/ Apptheme "> <activity android:name=". Main "android:label=" Fill in user information "> <intent-filter> <action android:name=" Android Oid.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> <activity android:name= ". ACTIVITY02 "android:label=" Display user Information > </activity> </application></manifest> </pre></pre><span style="font-size:14px"><span style="font-size:14px"><br>Note that the Android:label property is used to specify the name that is displayed on the title bar, and if activity sets the property, jumps to the activity<br>The title bar of the page is displayed in the Activity's configuration name, otherwise the configuration name displayed</span> in the application</span>。<br></pre><br><p><p></p></p><p><p><br></p></p><p><p><br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Self-Study The data transfer case in Android note--activity (user Registration)</p></p></span>

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.