Android has an Activity that returns results

Source: Internet
Author: User

package com.example.android.active;import android.app.activity;import android.content.intent; import android.os.bundle;import android.view.view;import android.view.view.onclicklistener; Import android.widget.button;import android.widget.edittext;/** * activity implements the return result  *  1. The return result of the activity needs to be obtained and another activity * 2 must be started using the Startactivityforresult () method. You must override the Onactivityresult () method to process the returned result  * 3. To use the Setresult () method to set the results in the activity that returns the results  *  * */public class  Mainactivity3 extends activity implements onclicklistener{private button button1 ;p rivate edittext etnumber;private static final int requestcode=1;     //the request code, just make a token to identify the @overrideprotected void oncreate in Onactivityresult () (bundle  Savedinstancestate)  {// todo auto-generated method stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.ACTIVITY_MAIN3); button1= (Button)  findviewbyid (r.id.submit1); Button1.setonclicklistener (this); EtNumber= ( EditText)  findviewbyid (r.id.editphonenumber);} @Overridepublic  void onclick (view v)  {// TODO Auto-generated method  stub//Start a activityintent intent=new intent (This,mainactivity4.class) with a return result;//parameter: 1.intent object    2. The request encoding (token) can be a positive integer value Startactivityforresult (Intent, requestcode);} Override methods that return results @overrideprotected void onactivityresult (Int requestcode, int resultcode,  intent data)  {// todo auto-generated method stubsuper.onactivityresult ( Requestcode, resultcode, data);switch  (Requestcode)  {case requestcode:if (resultCode== RESULT_OK) {String phone=data.getstringextra ("phone"); Etnumber.settext (phone);} Break;default:break;}}}
package com.example.android.active;import android.app.activity;import android.content.intent; Import android.os.bundle;import android.view.view;import android.widget.adapterview;import  android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import  Android.widget.listview;import android.widget.textview;public class mainactivity4 extends  Activity implements OnItemClickListener{private ListView listView; @Overrideprotected  void oncreate (bundle savedinstancestate)  {// todo auto-generated method  stubsuper.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_MAIN4); listView= (ListView)   Findviewbyid (R.ID.LISTVIEW1); string [] array={"123", "334", "435"}; Arrayadapter<string> adapter=new arrayadapter<string> (this, android. R.layout.simple_list_item_single_choice,array); Listview.setadapter (adapter);Listview.setonitemclicklistener (this);} @Overridepublic  void onitemclick (adapterview<?> arg0, view v, int  ARG2,&NBSP;LONG&NBSP;ARG3)  {// todo auto-generated method stubtextview textview= (TextView) v; String s= (String)  textview.gettext (). toString (); System.out.println (s);//Set the returned result Intent intent=new intent (); Intent.putextra ("Phone",  s); This.setresult (result_ok, intent); This.finish ();}}

Activity_main3.xml

<?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"   >        <EditText          android:id= "@+id/editphonenumber"         android:layout_ Width= "Match_parent"         android:layout_height= "Wrap_content"          android:hint= "Please enter"                  />    <Button       android:id= "@+id/submit1"     android:layout_width= "Match_parent" Android: layout_height= "Wrap_content" Android:text= "Submit"             /></LinearLayout> 

Activity_main4

<?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 " > <listview android:id= "@+id/listview1" android:layout_width= "Match_parent" Android:layout_h eight= "Wrap_content" > </ListView></LinearLayout>

Google China

This article is from the "matengbing" blog, make sure to keep this source http://matengbing.blog.51cto.com/11395502/1881325

Android has an Activity that returns results

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.