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實(shí)現(xiàn)返回結(jié)果 * 1.需要得到activity的返回結(jié)果,必須使用startActivityForResult()方法啟動另一個(gè)activity * 2.必須重寫onActivityResult()方法來處理返回結(jié)果 * 3.在返回結(jié)果的activity中要使用setResult()方法設(shè)置結(jié)果 * * */ public class MainActivity3 extends Activity implements OnClickListener{ private Button button1; private EditText etNumber; private static final int REQUESTCODE=1; //請求編碼,只是做一個(gè)標(biāo)記,以便在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 //啟動一個(gè)有返回結(jié)果的Activity Intent intent=new Intent(this,MainActivity4.class); //參數(shù):1.intent對象 2.請求編碼(標(biāo)記)可以是正整數(shù)值 startActivityForResult(intent, REQUESTCODE); } //重寫返回結(jié)果的方法 @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"}; ArrayAdapteradapter=new ArrayAdapter (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); //設(shè)置返回的結(jié)果 Intent intent=new Intent(); intent.putExtra("phone", s); this.setResult(RESULT_OK, intent); this.finish(); } }
activity_main3.xml
蚌山網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),蚌山網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為蚌山近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個(gè)售后服務(wù)好的蚌山做網(wǎng)站的公司定做!
activity_main4
谷歌中國