其他分享
首页 > 其他分享> > 模仿手机发短信选择联系人电话的功能

模仿手机发短信选择联系人电话的功能

作者:互联网

API

相关常量
	联系人URI: Phone. CONTENT_URI
	姓名字段: Phone.DISPLAY_NAME
	号码字段: Phone. NUMBER

读取联系人权限:
	android.permission.READ_CONTACTS

主界面

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/et_main_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入或选择一个联系人号码" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_main_number"
        android:text="选择联系人" 
        android:onClick="toContactList"/>

</RelativeLayout>

package com.jane.App_provider;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity
{

	private EditText et_main_number;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		et_main_number = (EditText) findViewById(R.id.et_main_number);
	}

	public void toContactList(View v)
	{
		// 启动联系人列表界面
		startActivityForResult(new Intent(this, ContactListActivity.class), 1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == 1 && resultCode == RESULT_OK)
		{
			// 得到返回的number
			String number = data.getStringExtra("NUMBER");
			// 显示
			et_main_number.setText(number);
		}
	}
}

联系人列表界面

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</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:padding="5dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" 
        android:textSize="18sp"/>

    <TextView
        android:id="@+id/tv_item_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" 
        android:textSize="18sp"
        android:layout_marginLeft="10dp"/>

</LinearLayout>

package com.jane.App_provider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
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 ContactListActivity extends ListActivity implements OnItemClickListener
{

	private ListView listView;
	private ContactAdapter adapter;
	private List<Map<String, String>> data = new ArrayList<Map<String, String>>();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact_list);

		listView = getListView();
		adapter = new ContactAdapter();
		// 查询得到联系人表数据
		// ContentResolver
		ContentResolver resolver = getContentResolver();
		// 执行查询得到cursor
		String[] projection =
		{ Phone.DISPLAY_NAME, Phone.NUMBER };
		Cursor cursor = resolver.query(Phone.CONTENT_URI, projection, null,
				null, null);
		// 取出其中的数据保存到data
		while (cursor.moveToNext())
		{
			String name = cursor.getString(0);
			String number = cursor.getString(1);
			Map<String, String> map = new HashMap<String, String>();
			map.put("name", name);
			map.put("number", number);
			data.add(map);
		}
		// 显示列表
		listView.setAdapter(adapter);

		// 给listView添加item点击监听
		listView.setOnItemClickListener(this);

	}

	class ContactAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			return data.size();
		}

		@Override
		public Object getItem(int position)
		{
			return data.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			if (convertView == null)
			{
				convertView = View.inflate(ContactListActivity.this,R.layout.item_contact, null);
			}

			Map<String, String> map = data.get(position);
			TextView nameTV = (TextView) convertView.findViewById(R.id.tv_item_name);
			TextView nubmerTV = (TextView) convertView.findViewById(R.id.tv_item_number);
			nameTV.setText(map.get("name"));
			nubmerTV.setText(map.get("number"));

			return convertView;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id)
	{
		// 得到选择的号码
		String number = data.get(position).get("number");
		Intent intent = getIntent();
		intent.putExtra("NUMBER", number);
		// 设置结果
		setResult(RESULT_OK, intent);
		// 返回
		finish();
	}
}

标签:联系人,number,Override,发短信,import,android,data,public,模仿
来源: https://blog.csdn.net/qq_43416157/article/details/111314756