AutoCompleteTextView用法总结【Android】

以下是对AutoCompleteTextView的一些小小总结


1.AutoCompleteTextView用ArrayAdapter适配器:

布局文件:

main.xml

<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=".MainActivity" >
    <AutoCompleteTextView
        android:id="@+id/actv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="1" />
</RelativeLayout>

java文件:

package com.succ7.autocompletiondemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//主代码
		String[] arrs = new String[]{"abc","adaadfa","aadfdfda","adsfadf"};
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,arrs);
		AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv);
		actv.setAdapter(adapter);
	}
}



2.AutoCompleteTextView用CursorAdapter适配器:

布局文件:

<?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" >
        <AutoCompleteTextView
            android:id="@+id/actv_contact_number"
            android:layout_width="match_parent"
            android:hint="请输入号码"
            android:layout_height="wrap_content"
            android:completionThreshold="1"
            android:background="@drawable/conversation_detail_input_selector" />
</LinearLayout>


<?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_select_contact_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="name"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/tv_select_contact_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="address"
        android:textColor="#00ff00"
        android:textSize="18sp" />

</LinearLayout>


java文件:

package com.succ7.smartsms;

import android.app.Activity;
import android.content.Context;
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.AutoCompleteTextView;
import android.widget.CursorAdapter;
import android.widget.FilterQueryProvider;
import android.widget.TextView;

public class CopyOfNewSmsActivity extends Activity {
	private AutoCompleteTextView actv_contact_number;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_sms);
		setTitle("新建信息");
		init();
	}

	private void init() {
		actv_contact_number = (AutoCompleteTextView) findViewById(R.id.actv_contact_number);
		
		NewMsgCursorAdapter adapter = new NewMsgCursorAdapter(this, null);
		actv_contact_number.setAdapter(adapter);
		adapter.setFilterQueryProvider(new FilterQueryProvider() {
			//当输入框中的内容发生变化时回调上方法
			@Override
			public Cursor runQuery(CharSequence constraint) {
				String[] projection = new String[]{
						"display_name",
						"data1",
						"_id"
				};
				Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, projection, "data1 like ?", new String[]{constraint+"%"}, null);
				//将cursor对象返回给adapter
				return cursor;
			}
		});

	}
	//为自动完成的输入框定义的cursoradapter
	class NewMsgCursorAdapter extends CursorAdapter{

		public NewMsgCursorAdapter(Context context, Cursor c) {
			super(context, c);
			// TODO Auto-generated constructor stub
		}
		@Override
		public CharSequence convertToString(Cursor cursor) {
			return cursor.getString(1);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			String name = cursor.getString(0);
			String address = cursor.getString(1);
			TextView tv_select_contact_name = (TextView) view.findViewById(R.id.tv_select_contact_name);
			TextView tv_select_contact_address = (TextView) view.findViewById(R.id.tv_select_contact_address);
			tv_select_contact_name.setText(name);
			tv_select_contact_address.setText(address);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = View.inflate(context, R.layout.item_select_contact, null);
			return view;
		}
	}
}






你可能感兴趣的:(Android开发,Cursor,布局)