zoukankan      html  css  js  c++  java
  • Android学习(三) 自动完成的使用

    1、AutoCompleteTextView

      自动完成功能,在文本框中输入字符,会出现匹配的自动提示。类似百度搜索。

    XML代码

    <?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" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="城市:" />
    
        <AutoCompleteTextView
            android:id="@+id/actv1"
            android:completionThreshold="3"    //输入多少个字符会出现提示
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="请输入城市" >
        </AutoCompleteTextView>
        
    </LinearLayout >

    Java代码

    public class AutoCompleteTextViewDemo extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.autocomplete_demo);
    
            //1、获取页面上的自动完成控件
            AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv1);
            //2、创建一个数组,保存的数据,字符串数组或者List<String>
            String[] city = new String[] { "beijing1", "shenzhen1", "shenzhen2", "beijing2", "shanghai" };
            //3、创建一个适配器对象,用来绑定数据到AutoCompleteTextView中的第一个为当前Activity对象,第二个参数为显示的样式,第三个为数据源.
            ArrayAdapter<String> citydatapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, city);
            //4、将适配器绑定到AutoCompleteTextView中
            actv.setAdapter(citydatapter);
        }
    }

    2、MultiAutoCompleteTextView

      支持多选的自动完成,可以实现类似邮箱收件人的多选效果。

    XML代码

    <?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" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_marginTop="20dp">
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="城市:"
                android:textSize="20sp" />
    
            <MultiAutoCompleteTextView
                android:id="@+id/mactv1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:completionThreshold="2"
                android:ems="10"
                android:hint="请输入城市" />
        </LinearLayout>
    
    </LinearLayout>

    Java代码:

    public class AutoCompleteTextViewDemo extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.autocomplete_demo);
    
            //1、获取页面上的自动完成控件
            MultiAutoCompleteTextView mactv = (MultiAutoCompleteTextView) findViewById(R.id.mactv1);
            //2、创建一个数组,保存的数据,字符串数组或者List<String>
            String[] city = new String[] { "beijing1", "shenzhen1", "shenzhen2", "beijing2", "shanghai" };
            //3、创建一个适配器对象,用来绑定数据到AutoCompleteTextView中的第一个为当前Activity对象,第二个参数为显示的样式,第三个为数据源.
            ArrayAdapter<String> citydatapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, city);
            //4、将适配器绑定到AutoCompleteTextView中
            mactv.setAdapter(citydatapter);        
            //5、设置分隔符,以,进行分割
            mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
        }
    }
  • 相关阅读:
    详细介绍C++STL:unordered_map
    NYOJ-626-intersection set(二分查找)
    hdoj-2141-Can you find it?(二分查找)
    HDU-1232-畅通工程(并查集)
    HDU-1213-How Many Tables(并查集)
    hdoj-2647-Reward(拓扑排序)
    hdoj-3342-Legal or Not(拓扑排序)
    hdoj-1285-确定比赛名次(拓扑排序)
    来一些方便的小操作:博客园(cnblog)自定义界面
    POJ--1094--Sorting It All Out||NYOJ--349--Sorting It All Out(拓扑排序)
  • 原文地址:https://www.cnblogs.com/zhengcheng/p/4351343.html
Copyright © 2011-2022 走看看