自制通讯录

这次的通讯录主要包括手机联系人列表展现,新建联系人两个功能。大概结构如下:

首先上联系人列表xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fcfcfc"
    android:orientation="vertical"  >

   		<RelativeLayout 
  			android:layout_width="fill_parent"
  			android:layout_height="45dp"
  			android:id="@+id/title" 
  			android:background="#3370CC"
  			android:gravity="center_vertical"  >  
  			
            
      		<TextView
        		android:layout_width="wrap_content" 
        		android:layout_height="wrap_content" 
        		android:text="联系人"
        		android:layout_centerInParent="true"
        		android:textSize="20sp"		
				android:textColor="#ffffff" /> 
			<ImageButton 
			    android:id="@+id/right_btn"
			    android:layout_width="100px" 
        		android:layout_height="wrap_content"
        		android:layout_alignParentRight="true" 
        		android:layout_centerVertical="true"
        		android:layout_marginRight="5dp"
        		android:src="@drawable/title_btn_function"
				android:background="@drawable/xj"
				android:onClick="newPer"
			    />      
		</RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@drawable/search_bar_bg" >

            <EditText
                android:id="@+id/editText1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="搜索"
                android:singleLine="true"
                android:focusable="false"
                android:focusableInTouchMode="true"
                android:textColor="#000"
                android:drawableLeft="@drawable/search_bar_icon_normal" 
                android:background="@drawable/search_bar_edit_bg" >               
            </EditText>
            
        </LinearLayout>
        
        <RelativeLayout  
    		android:layout_width="fill_parent"  
   			android:layout_height="fill_parent"> 
	        <ListView 
	            android:layout_width="fill_parent" 
	          	android:layout_height="wrap_content"
	          	android:layout_marginRight="22dp" 
	          	android:id="@+id/ListView01">
	            
	        </ListView>
	        <com.example.shabao.SideBar  
			    android:id = "@+id/sideBar"  
			    android:layout_height="fill_parent"  
			    android:layout_width="22dp"
			    android:layout_alignParentRight="true"  
			/> 
        
        </RelativeLayout>
        

</LinearLayout>
再来看MainActivity:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.SimpleAdapter;
/**
 * 
 * @author 百世经纶
 *
 */
@SuppressLint("DefaultLocale")
public class MainActivity extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_list);
		
		 ListView list = (ListView) findViewById(R.id.ListView01);
	        
	        //生成动态数组,加入数据
	        ArrayList<HashMap<String, Object>> listItem 
	        	= new ArrayList<HashMap<String, Object>>();
	        //用TreeMap可以实现按首字母排序,不需要另外再做排序操作
	        Map<String,ArrayList<ContactInfo>> maplist = new TreeMap<String,ArrayList<ContactInfo>>();
	        ContentResolver resolver = this.getContentResolver();
	        maplist = Util.getContact(resolver);
	        ArrayList<ContactInfo> value = new ArrayList<ContactInfo>();
	        Iterator<String> it=maplist.keySet().iterator();
	        String key = "";
	        while(it.hasNext()){
	        	key = it.next().toString();    
	            value = maplist.get(key);
	            listItem.add(Util.addC(key));
	            for(ContactInfo c :value){
		        	listItem.add(Util.addC(c));
            	}
	           
	        }

	        //生成适配器的Item和动态数组对应的元素
	        MyListAdapter listItemAdapter = new MyListAdapter(this,listItem,//数据源 
	            R.layout.childlayout,//ListItem的XML实现
	            //动态数组与ImageItem对应的子项        
	            new String[] {"ItemImage","ItemTitle", "ItemText", "key"}, 
	            //ImageItem的XML文件里面的一个ImageView,两个TextView ID
	            new int[] {R.id.contactIcon,R.id.name,R.id.description}
	        );
	       
	        //添加并且显示
	        list.setAdapter(listItemAdapter);
	        SideBar indexBar = (SideBar) findViewById(R.id.sideBar);  
	        indexBar.setListView(list); 
	      
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void newPer(View v){
		Intent intent = new Intent(MainActivity.this, Contact_CreatActivity.class);
		startActivity(intent);
		MainActivity.this.finish();
	}
	
	




}
里面用到的取通讯录数据的方法在Util文件中,我在里面建了27个线性列表不知道是不是还有更简便的写法,

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.TextUtils;
/**
 * 
 * @author 百世经纶
 *
 */
public class Util {

	//还原11位手机号
	public static String GetNumber(String num){
		if(num!=null)
		{
		if (num.startsWith("+86"))
        {
			num = num.substring(3);
        }
        else if (num.startsWith("86")){
        	num = num.substring(2);
        }
		}
		else{
			num="";
		}
		return num;
	}
	
/**
 * 读取手机上的联系人
 * @param r
 * @return
 */
	public static Map<String,ArrayList<ContactInfo>> getContact(ContentResolver r) {
		 Map<String,ArrayList<ContactInfo>> listMembers = new TreeMap<String,ArrayList<ContactInfo>>();
		 ArrayList<ContactInfo> ma = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mb = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mc = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> md = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> me = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mf = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mg = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mh = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mi = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mj = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mk = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> ml = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mm = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mn = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mo = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mp = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mq = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mr = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> ms = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mt = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mu = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mv = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mw = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mx = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> my = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> mz = new ArrayList<ContactInfo>();
		 ArrayList<ContactInfo> other = new ArrayList<ContactInfo>();
		 Cursor cursor = null;
		 try { 
//			 ContentResolver resolver = this.getContentResolver();   
			 cursor= r.query(Phone.CONTENT_URI,  
		                new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME,  
                        Phone.NUMBER, Phone.SORT_KEY_PRIMARY },  
                null, null, Phone.SORT_KEY_PRIMARY); 
			 if (cursor != null) {  
			        while (cursor.moveToNext()) { 
			        	ContactInfo contact = new ContactInfo();
			            String number = cursor.getString(2);  
			            // 当手机号码为空的或者为空字段 跳过当前循环    
			            if (TextUtils.isEmpty(number))    
			                continue;  
			            // 得到联系人名称                
			            String username = cursor.getString(1);
			            String sortKey = getSortKey(cursor.getString(3));
			            int id = cursor.getInt(0);
			            contact.setContactName(username);
			            contact.setSortKey(sortKey);
			            contact.setUserNumber(number);
			            contact.setId(id);
			            if(sortKey.equals("A")){
			            	 ma.add(contact);
			            }else if(sortKey.equals("B")){
			            	 mb.add(contact);
			            }else if(sortKey.equals("C")){
			            	 mc.add(contact);
			            }else if(sortKey.equals("D")){
			            	 md.add(contact);
			            }else if(sortKey.equals("E")){
			            	 me.add(contact);
			            }else if(sortKey.equals("F")){
			            	 mf.add(contact);
			            }else if(sortKey.equals("G")){
			            	 mg.add(contact);
			            }else if(sortKey.equals("H")){
			            	 mh.add(contact);
			            }else if(sortKey.equals("I")){
			            	 mi.add(contact);
			            }else if(sortKey.equals("J")){
			            	 mj.add(contact);
			            }else if(sortKey.equals("K")){
			            	 mk.add(contact);
			            }else if(sortKey.equals("L")){
			            	 ml.add(contact);
			            }else if(sortKey.equals("M")){
			            	 mm.add(contact);
			            }else if(sortKey.equals("N")){
			            	 mn.add(contact);
			            }else if(sortKey.equals("O")){
			            	 mo.add(contact);
			            }else if(sortKey.equals("P")){
			            	 mp.add(contact);
			            }else if(sortKey.equals("Q")){
			            	 mq.add(contact);
			            }else if(sortKey.equals("R")){
			            	 mr.add(contact);
			            }else if(sortKey.equals("S")){
			            	 ms.add(contact);
			            }else if(sortKey.equals("T")){
			            	 mt.add(contact);
			            }else if(sortKey.equals("U")){
			            	 mu.add(contact);
			            }else if(sortKey.equals("V")){
			            	 mv.add(contact);
			            }else if(sortKey.equals("W")){
			            	 mw.add(contact);
			            }else if(sortKey.equals("X")){
			            	 mx.add(contact);
			            }else if(sortKey.equals("Y")){
			            	 my.add(contact);
			            }else if(sortKey.equals("Z")){
			            	 mz.add(contact);
			            }else if(sortKey.equals("#")){
			            	 other.add(contact);
			            }
			            
			        }
			        listMembers.put("A", ma);
			        listMembers.put("B", mb);
			        listMembers.put("C", mc);
			        listMembers.put("D", md);
			        listMembers.put("E", me);
			        listMembers.put("F", mf);
			        listMembers.put("G", mg);
			        listMembers.put("H", mh);
			        listMembers.put("I", mi);
			        listMembers.put("J", mj);
			        listMembers.put("K", mk);
			        listMembers.put("L", ml);
			        listMembers.put("M", mm);
			        listMembers.put("N", mn);
			        listMembers.put("O", mo);
			        listMembers.put("P", mp);
			        listMembers.put("Q", mq);
			        listMembers.put("R", mr);
			        listMembers.put("S", ms);
			        listMembers.put("T", mt);
			        listMembers.put("U", mu);
			        listMembers.put("V", mv);
			        listMembers.put("W", mw);
			        listMembers.put("X", mx);
			        listMembers.put("Y", my);
			        listMembers.put("Z", mz);
			        listMembers.put("#", other);
			    } 
		 }catch(Exception e){
			 e.printStackTrace();
		 }finally{
			 cursor = null;
		 }

		
		return listMembers;
		
	}
	
	/**
	* 获取sort key的首个字符,如果是英文字母就直接返回,否则返回#。
	* 
	* @param sortKeyString
	* 数据库中读取出的sort key
	* @return 英文字母或者#
	*/
	private static String getSortKey(String sortKeyString) {
		String key = sortKeyString.substring(0, 1).toUpperCase();
		if (key.matches("[A-Z]")) {
			 return key;
		}
		return "#";
	} 

	public static HashMap<String, Object> addC(ContactInfo c){
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ItemImage", R.drawable.h015);//图像资源的ID
    	map.put("ItemTitle", c.getContactName());
    	map.put("ItemText", c.getUserNumber());
    	map.put("key", c.getSortKey());
		return map;
		
	}
	
	public static HashMap<String, Object> addC(String key){
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ItemImage", null);//图像资源的ID
    	map.put("ItemTitle", key);
    	map.put("ItemText", null);
    	map.put("key", key);
		return map;
		
	}


}
接下去是 适配器和索引,索引这一块,之前还不知道怎么处理,参考了下面这篇博客才完成的: http://blog.csdn.net/ilysony/article/details/6292771

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MyListAdapter extends SimpleAdapter implements SectionIndexer {

	private View myView;
	private TextView name, num;
	private ImageView img;
	private LayoutInflater layoutinflater;
	ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
	int layoutId;
	String[] from;
	int[] to;
	@SuppressWarnings("unchecked")
	public MyListAdapter(Context context, List<? extends Map<String, ?>> data,
			int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		// TODO Auto-generated constructor stub
		this.listItem = (ArrayList<HashMap<String, Object>>) data;
		this.layoutId = resource;
		this.from = from;
		this.to = to;
		this.layoutinflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return super.getCount();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		myView = layoutinflater.inflate(R.layout.childlayout, null);
		name = (TextView) myView.findViewById(to[1]);
		num = (TextView) myView.findViewById(to[2]);
		img = (ImageView) myView.findViewById(to[0]);
		
//		Iterator<HashMap<String, Object>> it1 = listItem.iterator();
//		while(it1.hasNext()){
			HashMap<String, Object> map = new HashMap<String, Object>();
			map = listItem.get(position);
			if(map.get(from[0]) == null && map.get(from[2]) == null){//英文首字母
				name.setText(map.get(from[1]).toString());
				num.setVisibility(View.GONE);
				img.setVisibility(View.GONE);
				name.setTextSize(26);
				name.setBackgroundColor(Color.GRAY);
				if(listItem.get(position+1).get(from[3]).toString().equals(map.get(from[1]).toString())){
					name.setVisibility(View.VISIBLE);
				}else{//字母目录下没有列表项
					LinearLayout l1 = (LinearLayout) myView.findViewById(R.id.linearLayout1);
					l1.setVisibility(View.GONE);
					
				}
			}else{//正常联系人
				name.setText(map.get(from[1]).toString());
				img.setImageResource((Integer) map.get(from[0]));
				num.setText(map.get(from[2]).toString());
				num.setVisibility(View.VISIBLE);
				img.setVisibility(View.VISIBLE);
//				l1.setVisibility(View.VISIBLE);
				name.setTextSize(30);
			}
//		}


		return myView;
	}

	@Override
	public Object[] getSections() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getPositionForSection(int section) {
		// TODO Auto-generated method stub
		if (section == 35) {  
            return 0;  
        }  
        for (int i = 0; i < listItem.size(); i++) {  
            char[] firstChar = listItem.get(i).get(from[3]).toString().toCharArray();  
            if (firstChar[0] == section) {  
                return i;  
            }  
        }  
        return -1;  
	}

	@Override
	public int getSectionForPosition(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	

}
索引类:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.SectionIndexer;
/**
 * 
 * @author 百世经纶
 *
 */
public class SideBar extends View {
	private char[] l;
	private SectionIndexer sectionIndexter = null;
	private ListView list;
	private final int m_nItemHeight = 38;
	private int aa = 0;
	public SideBar(Context context) {
		super(context);
		init();
	}
	public SideBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	private void init() {
		l = new char[] {'#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
				'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
		setBackgroundColor(0x44FFFFFF);
	}
	public SideBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	public void setListView(ListView _list) {
		list = _list;
		sectionIndexter = (SectionIndexer) _list.getAdapter();
	}
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		int i = (int) event.getY();
		int idx = i / m_nItemHeight;
		if (idx >= l.length) {
			idx = l.length - 1;
		} else if (idx < 0) {
			idx = 0;
		}
		//取得当前按下的数组下标
		aa = idx;
		invalidate();//重新绘制界面
		if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
			if (sectionIndexter == null) {
				sectionIndexter = (SectionIndexer) list.getAdapter();
			}
			//获取列表的位置
			int position = sectionIndexter.getPositionForSection(l[idx]);
			if (position == -1) {
				return true;
			}
			//将列表焦点移到当前位置
			list.setSelection(position);
		}
		return true;
	}

	protected void onDraw(Canvas canvas) {
		Paint paint = new Paint();
		paint.setColor(0xFFA6A9AA);
		paint.setTextSize(30);
		paint.setTextAlign(Paint.Align.CENTER);
		float widthCenter = getMeasuredWidth() / 2;
		for (int i = 0; i < l.length; i++) {
			if(i == aa){//绘制索引,按下的颜色加以区别
				paint.setColor(Color.GREEN);
				canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint);
			}else{
				paint.setColor(0xFFA6A9AA);
				canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint);
			}
		}
		super.onDraw(canvas);
	}
}
里面有用到的列表项xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:paddingTop="0.0dip"
  android:paddingBottom="0.0dip">
  <ImageView 
  		android:id="@+id/contactIcon"
  		android:padding="4.0dip"
  		android:layout_width="48.0dip"
		android:layout_height="48.0dip"
		android:scaleType="fitCenter"
  		android:layout_marginLeft="10.0dip"
		android:layout_marginTop="5.0dip"
		android:layout_marginBottom="5.0dip"
  		android:src="@drawable/h015"
  		/>
  <LinearLayout
      	android:id="@+id/linearLayout1"
  		android:layout_height="wrap_content"
  		android:layout_weight="1"
  		android:layout_width="fill_parent"
  		android:layout_marginLeft="5.0dip"
		android:layout_marginTop="9.0dip"
		android:layout_marginBottom="9.0dip"
  		android:orientation="vertical">
  		
  		<TextView
  			android:textColor="#000000"
  			android:textSize="22sp"
  			android:id="@+id/name"
  			android:layout_marginRight="5.0dip"
  			android:layout_width="fill_parent"
  			android:layout_height="wrap_content"
  			android:singleLine="true"
  			android:text="name"
  			
  		/>
  		
  		<TextView android:id="@+id/description"
         android:textSize="18sp"
         android:textColor="#cccccc"
         android:text="13819881668"
         android:layout_marginTop="10dp"
         android:ellipsize="marquee"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginRight="5.0dip"
         android:singleLine="true"
		 android:marqueeRepeatLimit="1"
         />
  		
  </LinearLayout>

  
</LinearLayout>

最后是新建联系人页面,界面比较丑,凑合着看吧。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fcfcfc"
    android:orientation="vertical"  >

   		<RelativeLayout 
  			android:layout_width="fill_parent"
  			android:layout_height="45dp"
  			android:id="@+id/title" 
  			android:background="#3370CC"
  			android:gravity="center_vertical"  >  
  			
            
      		<TextView
        		android:layout_width="wrap_content" 
        		android:layout_height="wrap_content" 
        		android:text="新建联系人"
        		android:layout_centerInParent="true"
        		android:textSize="20sp"		
				android:textColor="#ffffff" /> 
			<TextView 
			    android:id="@+id/baocun"
			    android:layout_width="100dp" 
        		android:layout_height="wrap_content"
        		android:layout_alignParentRight="true" 
        		android:layout_centerVertical="true"
        		android:layout_marginRight="10dp"
        		android:gravity="right"
        		android:text="保存"
				android:onClick="newPer"
				android:textSize="20sp"		
				android:textColor="#ffffff"
			    />      
	   </RelativeLayout>

       <LinearLayout android:orientation="horizontal"
				android:layout_width="fill_parent"    
				android:layout_height="wrap_content">
		   <TextView 
		      	android:layout_height="wrap_content"
		       android:layout_width="wrap_content"
		       android:layout_marginTop="5dip"
		       android:layout_marginLeft="10dip"
		       android:layout_marginRight="10dip"
		       android:text="姓名:"
		       android:layout_gravity="center"/>
		   <EditText
		       android:id="@+id/sr_name"
		       android:layout_height="wrap_content"
		       android:layout_width="fill_parent"
		       android:layout_marginLeft="10dip"
		       android:layout_marginRight="10dip"
		       android:scrollHorizontally="true"
		       android:autoText="false"/>
		   </LinearLayout>
        <LinearLayout android:orientation="horizontal"
				android:layout_width="fill_parent"    
				android:layout_height="wrap_content">
		   <TextView 
		      	android:layout_height="wrap_content"
		       android:layout_width="wrap_content"
		       android:layout_marginTop="5dip"
		       android:layout_marginLeft="10dip"
		       android:layout_marginRight="10dip"
		       android:text="手机:"
		       android:layout_gravity="center"/>
		   <EditText
		       android:id="@+id/sr_num"
		       android:layout_height="wrap_content"
		       android:layout_width="fill_parent"
		       android:layout_marginLeft="10dip"
		       android:layout_marginRight="10dip"
		       android:scrollHorizontally="true"
		       android:autoText="false"/>
		   </LinearLayout>
		 
       

</LinearLayout>
相应的Activity:

import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
/**
 * 
 * @author 百世经纶
 *
 */
@SuppressWarnings("deprecation")
public class Contact_CreatActivity extends Activity {

	EditText name, num;
	TextView baocun;
//	OnClickListener l;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_create);
		
		name = (EditText) findViewById(R.id.sr_name);
		num = (EditText) findViewById(R.id.sr_num);
		baocun = (TextView) findViewById(R.id.baocun);
		baocun.setOnClickListener(l);
		
	        
	}

	private OnClickListener l = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId()){
				case R.id.baocun:
					String uname = name.getText().toString(); 
					String unum = num.getText().toString();
					if(uname != null && !uname.equals("") && unum !=null && !unum.equals("")){
						Uri u = insertContact(Contact_CreatActivity.this,uname,unum);
						System.out.println(u);
						new AlertDialog.Builder(Contact_CreatActivity.this)
						.setTitle("提示")
						.setMessage("新增联系人成功!")
						.setPositiveButton("确定", new DialogInterface.OnClickListener(){
	
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								name.setText("");
								num.setText("");
							}
						})
						.show();
					}else {
						new AlertDialog.Builder(Contact_CreatActivity.this)
						.setTitle("提示")
						.setMessage("新增联系人姓名、手机号码不能为空!")
						.setPositiveButton("确定", null)
						.show();
					}
					
	
				case R.id.delete:
					break;
				case R.id.newsome:
					break;
				default:
					break;
			}
		}
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	/**
	 * 插入联系人
	 */
	private Uri insertContact(Context context, String name, String phone) {
		   
	       ContentValues values = new ContentValues();
	       values.put(People.NAME, name);
	       Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
	       Uri numberUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
	       values.clear();
	       
	       values.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);
	       values.put(People.NUMBER, phone);
	       getContentResolver().insert(numberUri, values);
	       
	       return uri;
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if (event.getRepeatCount() == 0) { // 按下的如果是BACK,同时没有重复
				Intent intent = new Intent();
				intent.setClass(Contact_CreatActivity.this, MainActivity.class);
				startActivity(intent); 
				Contact_CreatActivity.this.finish();
			}
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

}
最后不要忘记加权限:

<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
好了,看下运行的结果,以三星9300为准:



点一下索引,列表焦点下移


以上就是全部内容了,但还有一个问题:上拉左边的list,拉到其他索引位置的时候,怎么让索引的绿色光标跟着动。有兴趣的朋友可以交流一下。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值