android使用AutoCompleteTextView实现自动完成
解决方法:
1、activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/root"
tools:context="com.example.ff.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="国家列表" />
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:completionThreshold="1"
android:completionHint="请输入一个国家名字"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:ems="10">
<requestFocus/>
</AutoCompleteTextView>
</RelativeLayout>
2、在string.xml的<resources>之间放以下代码:
<string-array name="countries_array">
<item>American</item>
<item>China</item>
</string-array>
3、在string.xml文件的相同目录下新建line_item.xml,line_item.xml内容:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textColor="#000"
android:textSize="16sp" >
</TextView>
4、MainActivity.java
package com.example.ff;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
AutoCompleteTextView auto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到自动完成对象
auto = (AutoCompleteTextView) findViewById(R.id.autoComplete);
//从资源文件中得到数组
String[] countries = getResources().getStringArray(R.array.countries_array);
//使用ArrayAdapter.R.layout.line_item表示每一行的样子
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.line_item,countries);
//绑定AutoCompleteTextView与ArrayAdapter
auto.setAdapter(adapter);
//加事件
auto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
/*
* parent:适配器元素,控制器
* view:AutoCompleteTextView本身
* position:位置从0开始
* id:每一项唯一的值,默认与position相同
*/
Toast.makeText(MainActivity.this, "你选中了"+parent.getItemAtPosition(position), Toast.LENGTH_LONG)
.show();
}
});
}
}
本文地址:http://www.yayihouse.com/yayishuwu/chapter/1149