您好,登錄后才能下訂單哦!
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實現返回結果 * 1.需要得到activity的返回結果,必須使用startActivityForResult()方法啟動另一個activity * 2.必須重寫onActivityResult()方法來處理返回結果 * 3.在返回結果的activity中要使用setResult()方法設置結果 * * */ public class MainActivity3 extends Activity implements OnClickListener{ private Button button1; private EditText etNumber; private static final int REQUESTCODE=1; //請求編碼,只是做一個標記,以便在onActivityResult()中識別 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); button1=(Button) findViewById(R.id.submit1); button1.setOnClickListener(this); etNumber=(EditText) findViewById(R.id.EditPhoneNumber); } @Override public void onClick(View v) { // TODO Auto-generated method stub //啟動一個有返回結果的Activity Intent intent=new Intent(this,MainActivity4.class); //參數:1.intent對象 2.請求編碼(標記)可以是正整數值 startActivityForResult(intent, REQUESTCODE); } //重寫返回結果的方法 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.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; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.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); } @Override public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) { // TODO Auto-generated method stub TextView textView=(TextView)v; String s=(String) textView.getText().toString(); System.out.println(s); //設置返回的結果 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="請輸入" /> <Button android:id="@+id/submit1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" /> </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_height="wrap_content" > </ListView> </LinearLayout>
谷歌中國
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。