1. 视图示例, 按选择弹出 2界面, 选择选项 回显到1
2. 示例代码
MainActivity.java, 第一个activity
public class MainActivity extends Activity {
private EditText et_number;
private EditText et_number2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_number = (EditText) findViewById(R.id.et_number);
et_number2 = (EditText) findViewById(R.id.et_number2);
} /**
* 激活一个新的界面 让用户选择联系人
*
* @param view
*/
public void selectContact(View view) {
Intent intent = new Intent(this, SelectContactActivity.class);
// 开启一个新的activity
// startActivity(intent);
// 开启一个新的activity 并且获取这个新开启的activity执行完毕后返回的结果
startActivityForResult(intent, 1);
} /**
* 激活一个新的界面 让用户选择第二个联系人
*
* @param view
*/
public void selectContact2(View view) {
Intent intent = new Intent(this, SelectContactActivity.class);
// 开启一个新的activity
// startActivity(intent);
// 开启一个新的activity 并且获取这个新开启的activity执行完毕后返回的结果
startActivityForResult(intent, 2);
} // 当新开启的activity 关闭的时候 调用的方法.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("onActivityResult"); if (data != null) {
String number = data.getStringExtra("number");
if (requestCode == 1) {
et_number.setText(number);
}else if(requestCode==2){
et_number2.setText(number);
}
} super.onActivityResult(requestCode, resultCode, data);
} }
SelectContactActivity.java 第二个activity
public class SelectContactActivity extends Activity {
private ListView lv_select_contact;
private List<ContactInfo> contactInfos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_contact);
lv_select_contact = (ListView) findViewById(R.id.lv_select_contact);
contactInfos = ContactInfoService.getContactInfos(this);
lv_select_contact.setAdapter(new ContactAdapter()); //给listview的条目设置 一下点击事件
lv_select_contact.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
ContactInfo info = contactInfos.get(position);
String number = info.getNumber(); //传递数据给调用他的activity
Intent data = new Intent();
data.putExtra("number", number);
setResult(100, data); //关闭当前的activity 然后传递数据给 调用者 调用者就会执行 onactivityResult的方法
finish();
}
}); } private class ContactAdapter extends BaseAdapter{ @Override
public int getCount() {
return contactInfos.size();
} @Override
public Object getItem(int position) {
return null;
} @Override
public long getItemId(int position) {
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ContactInfo info = contactInfos.get(position);
View view = View.inflate(getApplicationContext(), R.layout.contact_item, null);
TextView tv_name = (TextView) view.findViewById(R.id.tv_contact_name);
TextView tv_number = (TextView) view.findViewById(R.id.tv_contact_number);
tv_name.setText(info.getName());
tv_number.setText(info.getNumber());
return view;
} } }
ContactInfoService.java, 从db获取所有联系人信息
public class ContactInfoService {
/**
* 返回系统所有的联系人的信息
* @return
*/
public static List<ContactInfo> getContactInfos(Context context){
//创建一个系统所有联系人的集合
List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();
ContentResolver resolver = context.getContentResolver();
// raw_contact 表的uri
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
// data 表的uri
Uri dataUri = Uri.parse("content://com.android.contacts/data");
Cursor cursor = resolver.query(uri, new String[] { "contact_id" },
null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(0);
if (id != null) {
Cursor dataCursor = resolver.query(dataUri, new String[] {
"data1", "mimetype" }, "raw_contact_id=?",
new String[] { id }, null);
ContactInfo contactInfo = new ContactInfo();
while (dataCursor.moveToNext()) {
String data = dataCursor.getString(0);
String mimetype = dataCursor.getString(1);
if("vnd.android.cursor.item/name".equals(mimetype)){
contactInfo.setName(data);
}else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
contactInfo.setNumber(data);
}
}
contactInfos.add(contactInfo);
dataCursor.close();
}
}
cursor.close();
return contactInfos;
}
}
ContactInfo.java, 联系人javabean
public class ContactInfo {
private String name;
private String number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
} }
activity_main.xml, 第一个activity布局
<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=".MainActivity" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <EditText
android:id="@+id/et_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dip"
android:hint="请输入或者选择联系人" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="选择"
android:onClick="selectContact" />
</RelativeLayout> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <EditText
android:id="@+id/et_number2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dip"
android:hint="请输入或者选择第二个联系人" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="选择"
android:onClick="selectContact2" />
</RelativeLayout> <EditText
android:id="@+id/et_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dip"
android:hint="请输入 短信内容"
android:inputType="textMultiLine"
android:minLines="5" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="发送短信"
android:onClick="sendSms" >
</Button> </LinearLayout>
activity_select_contact.xml, 第二个activity布局
<?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_select_contact"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </LinearLayout>
contact_item.xml, ListView布局
<?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="wrap_content"
android:orientation="vertical" > <TextView
android:id="@+id/tv_contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="姓名"
android:textColor="#000000"
android:textSize="18sp" /> <TextView
android:id="@+id/tv_contact_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="电话"
android:textColor="#99000000"
android:textSize="14sp" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smssender"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_CONTACTS"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.smssender.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SelectContactActivity"
android:label="@string/select_contact"
></activity>
</application> </manifest>