返回Acitivty時需要傳遞數(shù),有以下幾個關(guān)鍵地方
- startActivityForResult(intent, SMS_LIST); 告訴系統(tǒng),這個activity:ContactActivity.class銷毀時,會返回?cái)?shù)據(jù)
- onActivityResult 方法的監(jiān)聽
- 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);
// 告訴系統(tǒng),這個activity:ContactActivity.class銷毀時,會返回?cái)?shù)據(jù)
startActivityForResult(intent, CONTACT_LIST);
}
public void quick(View v) {
Intent intent = new Intent(this, SMSActivity.class);
// startActivity(intent);
// 告訴系統(tǒng),這個activity:ContactActivity.class銷毀時,會返回?cái)?shù)據(jù)
startActivityForResult(intent, SMS_LIST);
}
// 當(dāng)一個應(yīng)用返回?cái)?shù)據(jù)的activity被銷毀了,此方法會調(diào)用,用戶接收數(shù)據(jù)
@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
// 虛構(gòu)幾個聯(lián)系人
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設(shè)置點(diǎn)擊監(jiān)聽,我們需要知道用戶什么時候點(diǎn)擊了條目,還要知道用戶點(diǎn)擊的條目
listView.setOnItemClickListener(new OnItemClickListener() {
// 用戶點(diǎn)擊了哪一個條目,通過這個參數(shù)知道
@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的調(diào)用者
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="請輸入聯(lián)系人" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="請選擇聯(lián)系人" />
</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="選擇快捷回復(fù)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="send"
android:text="發(fā)送" />
</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>