Intent回传值:启动一个Activity,当启动的Activity关闭时,传回值回到原Activity。以下一个小程序演示回传值。
package com.itheima.smshelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_content;
private EditText et_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_content = (EditText) findViewById(R.id.et_content);
et_number = (EditText) findViewById(R.id.et_number);
}
public void selectSms(View view) {
Intent intent = new Intent(this, ListSmsActivity.class);
// 开启一个新的界面,并且获取界面的返回值
// startActivity(intent);
// int requestCode 请求码
startActivityForResult(intent, 0);
}
public void selectNumber(View view) {
Intent intent = new Intent(this, ListNumberActivity.class);
// 开启一个新的界面,并且获取界面的返回值
// startActivity(intent);
// int requestCode 请求码
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
String smsinfo = data.getStringExtra("smsinfo");
if (resultCode == 0) {
et_content.setText(smsinfo);
} else if (resultCode == 1) {
et_number.setText(smsinfo);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void sendSms(View view){
String content = et_content.getText().toString().trim();
String number = et_number.getText().toString().trim();
SmsManager.getDefault().sendTextMessage(number, null, content, null, null);
Toast.makeText(this, "发送成功", 0).show();
}
}
package com.itheima.smshelper;
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 ListNumberActivity extends Activity {
private ListView lv;
private String[] objects = {
"1234","34324","5643543","32424" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter
R.id.tv_info, objects));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
String smsinfo = objects[position];
Intent data = new Intent();
data.putExtra("smsinfo", smsinfo);
//设置数据。
setResult(1, data);
//关闭掉当前的activity,并且回传数据 onActivityResult().
finish();
}
});
}
}
package com.itheima.smshelper;
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 ListSmsActivity extends Activity {
private ListView lv;
private String[] objects = {
"玫瑰香香,情人黏黏,情话甜甜,情歌绵绵;花灯灿灿,礼花点点,好运连连,好梦圆圆。情人节喜逢元宵节,喜鹊登枝蝴蝶成双鸳鸯成对双喜临门祝双节快乐,合家团团圆圆,甜甜蜜蜜,开开心心,幸幸福福",
"情人节快到了,我精心挑选玫瑰花、百合花和满天星,扎成一束鲜花随短信送给你,火红的玫瑰代表热烈奔放,洁白的百合代表百年好合,小小的满天星代表幸福美好。愿你的情人节热烈奔放,你们的爱情百年好合,你们的生活幸福美满。预祝情人节快乐",
"^o^满天星光,最爱你许过愿望的那一颗,鲜花绽放,最爱你摘下微笑的那一朵,曼妙旋律,最爱你感动落泪的那一段,亲爱的,情人节快乐,爱你。 ",
"^o^宝贝,情人节到了,送你一束玫瑰,用真心塑料纸包扎,系上快乐彩带,喷点爱的香水,插一张真情卡片,写着:宝贝,愿我的爱能带给你一生的快乐!" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter
R.id.tv_info, objects));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
String smsinfo = objects[position];
Intent data = new Intent();
data.putExtra("smsinfo", smsinfo);
//设置数据。
setResult(0, data);
//关闭掉当前的activity,并且回传数据 onActivityResult().
finish();
}
});
}
}
主界面
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入电话号码" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入短信的内容"
android:inputType="textMultiLine"
android:lines="5" />
可选短信
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="match_parent" >
ListView的Item布局
android:layout_height="wrap_content"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@android:drawable/star_big_on"
android:gravity="center_vertical"
android:text="我爱你。" />