返回Acitivty時需要傳遞數,有以下幾個關鍵地方
- startActivityForResult(intent, SMS_LIST); 告訴系統,這個activity:ContactActivity.class銷燬時,會返回資料
- onActivityResult 方法的監聽
- setResult(1, data);
// MainActivity.java
public class MainActivity extends Activity {
static final int CONTACT_LIST = 10;
static final int SMS_LIST = 20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
Intent intent = new Intent(this, ContactActivity.class);
// startActivity(intent);
// 告訴系統,這個activity:ContactActivity.class銷燬時,會返回資料
startActivityForResult(intent, CONTACT_LIST);
}
public void quick(View v) {
Intent intent = new Intent(this, SMSActivity.class);
// startActivity(intent);
// 告訴系統,這個activity:ContactActivity.class銷燬時,會返回資料
startActivityForResult(intent, SMS_LIST);
}
// 當一個應用返回資料的activity被銷燬了,此方法會呼叫,使用者接收資料
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
EditText et;
if (requestCode == CONTACT_LIST) {
et = (EditText) findViewById(R.id.et);
et.setText(data.getStringExtra("name"));
}
if (requestCode == SMS_LIST) {
et = (EditText) findViewById(R.id.content_et);
et.setText(data.getStringExtra("content"));
}
}
}
// ContactAcitivity.java
// 虛構幾個聯絡人
String[] objects = new String[] { "芙蓉", "四大皆空", "梅西", "傻逼" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
ListView listView = (ListView) findViewById(R.id.lv);
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.item_contact, R.id.tv, objects));
// 對ListView設定點選監聽,我們需要知道使用者什麼時候點選了條目,還要知道使用者點選的條目
listView.setOnItemClickListener(new OnItemClickListener() {
// 使用者點選了哪一個條目,通過這個引數知道
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String text = objects[position];
Intent data = new Intent();
data.putExtra("name", text);
// Activity一旦被銷燬,這個data就會傳到activity的呼叫者
setResult(0, data);
finish();
}
});
}
}
//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"
android:orientation="vertical"
tools:context="com.example.msgsend.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="請輸入聯絡人" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="請選擇聯絡人" />
</LinearLayout>
<EditText
android:id="@+id/content_et"
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:onClick="quick"
android:text="選擇快捷回覆" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="send"
android:text="傳送" />
</LinearLayout>
// 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="wrap_content" />
</LinearLayout>
// item_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" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>