import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.*; import android.widget.AdapterView.OnItemClickListener; public class AutoCompleteTextViewTest extends Activity { private LinearLayout mainLayout=null; private TextView tv0=null; private TextView tv1=null; private AutoCompleteTextView actv1=null; private TextView tv2=null; private AutoCompleteTextView actv2=null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mainLayout_init(); setContentView(mainLayout); } /*mainLayout初始化*/ void mainLayout_init(){ mainLayout=new LinearLayout(this); LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -1); mainLayout.setLayoutParams(lp); mainLayout.setOrientation(LinearLayout.VERTICAL); tv0_init(); tv1_init(); actv1_init(); tv2_init(); actv2_init(); mainLayout.addView(tv0); mainLayout.addView(tv1); mainLayout.addView(actv1); mainLayout.addView(tv2); mainLayout.addView(actv2); } /*tv0初始化*/ void tv0_init(){ tv0=new TextView(this); tv0.setText("这里显示操作结果\n"); tv0.append("----------华丽的分割线----------\n"); } /*tv1初始化*/ void tv1_init(){ tv1=new TextView(this); tv1.setText("这是英文自动填表"); } /*actv1初始化*/ void actv1_init(){ actv1=new AutoCompleteTextView(this); LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2); actv1.setLayoutParams(lp); //字符数组是内容 String str[]={"beijing","shanghai","guangzhou","shenzhen","xiamen","beijing1","beijing2","beijing3",}; //ArrayAdapter aa是中间联系设备 ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,str); //绑定 actv1.setAdapter(aa); //监听 OnItemClickListener oicl=new OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { tv0.setText("您选择了"+actv1.getText()+"\n"); tv0.append("----------华丽的分割线----------\n"); } }; actv1.setOnItemClickListener(oicl); } /*tv2初始化*/ void tv2_init(){ tv2=new TextView(this); tv2.setText("这是中文的自动填表"); } /*actv2初始化*/ void actv2_init(){ actv2=new AutoCompleteTextView(this); LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2); actv2.setLayoutParams(lp); String str[]={"北京","上海","广州","深圳","厦门","北京1","北京2","北京3",}; ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.select_dialog_item,str); actv2.setAdapter(aa); //监听 OnItemClickListener oicl=new OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { tv0.setText("您选择了"+actv2.getText()+"\n"); tv0.append("----------华丽的分割线----------\n"); } }; actv2.setOnItemClickListener(oicl); } }