标签:
应用场景:打开一个新的activity,在这个activity上获取数据,返回给打开它的界面
短信发送时,可以直接选择系统联系人
界面布局是一个线性布局,里面右侧选择联系人在EditText的右上,因此使用相对布局对输入框进行包裹,按钮使用android:layout_alignParentRight=”true”处理
下面的内容有多行,使用 属性android:inputType=”textMultiLine” 属性android:minLines=”5”
我们使用hvg的屏幕进行预览
打开一个新的界面展示系统联系人,采用ListView控件实现列表,继承BaseAdapter来实现适配器,通过ContentProvider读取系统的联系人。
MainActivity(主界面)
package com.tsh.gaojisms; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { private EditText et_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_number=(EditText) findViewById(R.id.et_number); } /** * 选择联系人 * @param v */ public void selectContacts(View v){ Intent intent=new Intent(this,SelectContactActivity.class); startActivityForResult(intent, 0); } /** * 接受返回的结果 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); String number=data.getStringExtra("number"); et_number.setText(number); } }
SelectContactActivity(联系人界面)
package com.tsh.gaojisms; import java.util.List; import com.tsh.gaojisms.domain.ContactInfo; import com.tsh.gaojisms.service.ContactInfosService; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class SelectContactActivity extends Activity { private ListView lv_contacts; public List<ContactInfo> contactInfos; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_select_contact); lv_contacts=(ListView) findViewById(R.id.lv_contacts); //获取数据 contactInfos=ContactInfosService.getContactInfos(this); lv_contacts.setAdapter(new MyAdapter()); //设置点击 lv_contacts.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ContactInfo info=contactInfos.get(position); String number=info.getNumber(); Intent intent=new Intent(); intent.putExtra("number", number); setResult(0,intent); finish(); } }); } /* * 适配器 */ private class MyAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return contactInfos.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view=View.inflate(SelectContactActivity.this, R.layout.contact_item, null); TextView tv_name=(TextView) view.findViewById(R.id.tv_name); TextView tv_number=(TextView) view.findViewById(R.id.tv_number); String name=contactInfos.get(position).getName(); String number=contactInfos.get(position).getNumber(); tv_name.setText(name); tv_number.setText(number); return view; }} }
ContactInfoService(获取联系人信息Service)
package com.tsh.gaojisms.service; import java.util.ArrayList; import java.util.List; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import com.tsh.gaojisms.domain.ContactInfo; public class ContactInfosService { public static List<ContactInfo> getContactInfos(Context context) { List<ContactInfo> contactInfos = new ArrayList<ContactInfo>(); ContentResolver resolver = context.getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri dataUri = Uri.parse("content://com.android.contacts/data"); // 循环联系人表 Cursor cursor = resolver.query(uri, null, null, null, null); while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex("contact_id")); if (id != null) { // 查找数据表 Cursor dataCursor = resolver.query(dataUri, null, "raw_contact_id=?", new String[] { id }, null); ContactInfo info = new ContactInfo(); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(dataCursor .getColumnIndex("data1")); String mimetype = dataCursor.getString(dataCursor .getColumnIndex("mimetype")); if (mimetype.equals("vnd.android.cursor.item/name")) { info.setName(data1); } else if (mimetype .equals("vnd.android.cursor.item/phone_v2")) { info.setNumber(data1); } } contactInfos.add(info); } } return contactInfos; } }
ContactInfo(联系人信息bean)
package com.tsh.gaojisms.domain; /** * 联系人业务bean * @author taoshihan * */ public class ContactInfo { private String name; private String number; public ContactInfo() { } public ContactInfo(String name, String number) { super(); this.name = name; this.number = number; } @Override public String toString() { return "ContactInfo [name=" + name + ", number=" + 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(主布局界面)
<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="${relativePackage}.${activityClass}" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/et_number" android:layout_marginTop="17dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入号码" /> <Button android:onClick="selectContacts" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择" /> </RelativeLayout> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="短信内容" android:inputType="textMultiLine" android:minLines="5"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送"/> </LinearLayout>
activity_select_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_contacts" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
contact_item.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:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_name" android:text="用户名" android:textSize="18sp" android:textColor="#ff0000"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_number" android:text="电话号码" android:textSize="12sp" android:textColor="#80000000"/> </LinearLayout>
这里开启activity需要用到一个新的api,startActivityForResult(intent,requestCode),开启一个新的activity并且获取这个activity执行完毕后返回的结果,参数:Intent对象,int类型请求码,此时用不到给个0
当新开启的activity关闭的时候,会调用onActivityResult()方法。传递过来的参数里面有个Intent对象,通过这个Intent对象获取到数据,展示到界面上
给ListView条目设置点击事件,调用ListView对象的setOnItemClickListener(listener)方法,参数:OnitemClickListener对象,它是一个接口类型,直接new这个接口,实现以下onItemClick(parent,view,id)方法,如果不知道函数的参数,我们可以采用断点调试的方法,查看一下参数代表的是什么值,调用ContactInfos对象的get(position)方法,得到ConatctInfo对象,调用ContactInfo对象的getNumber()方法,得到电话号码
传递数据给调用它的activity,调用setResult(requestCode,data)方法,参数:请求码,Intent对象,点击完成之后调用Activity对象的finish()方法
标签:
原文地址:http://www.cnblogs.com/taoshihan/p/5324380.html