有時(shí)候需要activity返回的時(shí)候攜帶數(shù)據(jù)返回,比如短信群發(fā),添加多個(gè)聯(lián)系人的時(shí)候,需要選擇listView里面的聯(lián)系人,同時(shí)把聯(lián)系人的數(shù)據(jù)返回。
首先需要在主activity里面設(shè)置意圖,進(jìn)行跳轉(zhuǎn),使用startActivityForResult而不是startActivity
Intent intent = new Intent(this, contactActivity.class); //啟動(dòng)選擇聯(lián)系人的activity //告訴系統(tǒng)這個(gè)activity返回時(shí)會(huì)返回?cái)?shù)據(jù),第二個(gè)參數(shù)是請(qǐng)求碼 startActivityForResult(intent, 10);跳轉(zhuǎn)到界面以后,封裝數(shù)據(jù)準(zhǔn)備攜帶數(shù)據(jù)返回
Intent data = new Intent(); //把要傳遞的數(shù)據(jù)封裝到intent對(duì)象 data.putExtra("name", objects[position]); //data就是intent攜帶的數(shù)據(jù),此activity一旦被銷(xiāo)毀data就會(huì)傳遞到此activity中 setResult(0, data); //第一個(gè)參數(shù)是響應(yīng)碼 //銷(xiāo)毀當(dāng)前的activity finish();設(shè)置條目監(jiān)聽(tīng)使用setOnItemClickListenerlv.setOnItemClickListener(new OnItemClickListener(){ //position:用戶(hù)點(diǎn)擊率哪一個(gè)條目 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } });獲取到封裝的數(shù)據(jù)返回,執(zhí)行onActivityResult(int requestCode, int resultCode, Intent data)來(lái)反饋獲取的數(shù)據(jù)當(dāng)一個(gè)應(yīng)該返回?cái)?shù)據(jù)的activity被銷(xiāo)毀的時(shí)候,此方法調(diào)用,用于接受數(shù)據(jù)
@Override PRotected void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode, data); }請(qǐng)求碼和響應(yīng)碼,主要是針對(duì)請(qǐng)求不同的activity用請(qǐng)求不同的activty,同時(shí)一個(gè)activity里面有不同的返回?cái)?shù)據(jù)需要返回碼來(lái)區(qū)分
示例代碼:activity_main.xml
<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=".MainActivity" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/et_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="請(qǐng)輸入聯(lián)系人" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請(qǐng)選擇聯(lián)系人" android:onClick="click1" /> </LinearLayout> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="5" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發(fā)送" android:onClick="click2" /></LinearLayout>MainActivity.javapackage com.ldw.contact;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click1(View v){ Intent intent = new Intent(this, contactActivity.class); //啟動(dòng)選擇聯(lián)系人的activity //告訴系統(tǒng)這個(gè)activity返回時(shí)會(huì)返回?cái)?shù)據(jù) startActivityForResult(intent, 10); } //當(dāng)一個(gè)應(yīng)該返回?cái)?shù)據(jù)的activity被銷(xiāo)毀的時(shí)候,此方法調(diào)用,用于接受數(shù)據(jù) @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ //根據(jù)請(qǐng)求碼判斷哪一個(gè)activity,接受哪一個(gè)activity返回的數(shù)據(jù) if(requestCode == 10){ EditText et_name = (EditText) findViewById(R.id.et_name); //獲取到傳遞過(guò)來(lái)的參數(shù) et_name.setText(data.getStringExtra("name")); } super.onActivityResult(requestCode, resultCode, data); } }activity_contact.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" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView></LinearLayout>item_contact.xml聯(lián)系人條目
<?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" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>contactActivity.java
package com.ldw.contact;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;public class contactActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); ListView lv = (ListView) findViewById(R.id.lv); final String[] objects = new String[]{ "A", "B", "C", "D" }; lv.setAdapter(new ArrayAdapter<String> (this, R.layout.item_contact, R.id.tv, objects)); //對(duì)listView設(shè)置條目點(diǎn)擊偵聽(tīng),知道什么時(shí)候點(diǎn)擊率條目,以及點(diǎn)擊了哪一個(gè)條目 lv.setOnItemClickListener(new OnItemClickListener(){ //position:用戶(hù)點(diǎn)擊率哪一個(gè)條目 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent data = new Intent(); //把要傳遞的數(shù)據(jù)封裝到intent對(duì)象 data.putExtra("name", objects[position]); //data就是intent攜帶的數(shù)據(jù),此activity一旦被銷(xiāo)毀data就會(huì)傳遞到此activity中 setResult(0, data); //銷(xiāo)毀當(dāng)前的activity finish(); } }); }}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注