AutoCompleteTextView自动完成文字输入

AutoCompleteTextView

Mian.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    >
<TextView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:text="@string/promptAutoCompleteText"
    />
<AutoCompleteTextView android:id="@+id/autCompTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    />
<Button android:id="@+id/btnAddAutoCompleteText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/promptBtnAddAutoCompleteText"
    />
<Button android:id="@+id/btnClrAutoCompleteText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/promptBtnClrAutoCompleteText"
    />
</LinearLayout>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />
</LinearLayout>

代码分析:

<AutoCompleteTextView android:id="@+id/autCompTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    />

AutoCompleteTextView 组件需要自定义一个id名称。

Main.java
package tw.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class Main extends Activity {
    private Button btnAddAutoCompleteText,
                   btnClrAutoCompleteText;
    private AutoCompleteTextView autCompTextView;

    private ArrayAdapter<String> adapAutoCompText;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        setupViewComponent();
    }
    
    private void setupViewComponent() {
        // 从R文件中确定接口组件
        btnAddAutoCompleteText = (Button)findViewById(R.id.btnAddAutoCompleteText);
        btnClrAutoCompleteText = (Button)findViewById(R.id.btnClrAutoCompleteText);
        autCompTextView = (AutoCompleteTextView)findViewById(R.id.autCompTextView);
        
        adapAutoCompText = new ArrayAdapter<String>(
                this, android.R.layout.simple_dropdown_item_1line);
        
        autCompTextView.setAdapter(adapAutoCompText);
        
        // 设置button组件的时间listener
        btnAddAutoCompleteText.setOnClickListener(btnAddAutoCompleteTextOnClickLis);
        btnClrAutoCompleteText.setOnClickListener(btnClrAutoCompleteTextOnClickLis);
    }
    
    private Button.OnClickListener btnAddAutoCompleteTextOnClickLis = new Button.OnClickListener() {
        public void onClick(View v) {
            String s = autCompTextView.getText().toString();
            adapAutoCompText.add(s);
            autCompTextView.setText("");
        }
    };
    
    private Button.OnClickListener btnClrAutoCompleteTextOnClickLis = new Button.OnClickListener() {
        public void onClick(View v) {
            adapAutoCompText.clear();
        }
    };
}

源代码

原文地址:https://www.cnblogs.com/zhoujn/p/4154053.html