zoukankan      html  css  js  c++  java
  • 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();
            }
        };
    }

    源代码

  • 相关阅读:
    MD5值算法原理
    AUTH过程
    锁定应用,解锁应用,锁卡,解卡,更改密码指令
    借/贷记卡的应用
    借记卡,贷记卡,准贷记卡三者的区别
    PBOC2.0与PBOC3.0的区别
    ED/EP简介
    与恒宝有关的一些常用知识
    java卡与native卡的区别
    计算机组和域的区别
  • 原文地址:https://www.cnblogs.com/zhoujn/p/4154053.html
Copyright © 2011-2022 走看看