• android 中使用AutoCompleteTextView 可以实现自动提示功能


    android 中使用AutoCompleteTextView 可以实现自动提示功能 

    在java文档中预先设置好需要提示的字串,默认设置是输入两个字符的时候提醒,但是可以在java文件里面使用setThreshold() 方法实现更改提醒是输入的字符数,但是提醒的文字需要预先匹配
    附上代码:
    xml文件:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools= "http://schemas.android.com/tools"   
       android:layout_width= "match_parent"  
       android:layout_height= "match_parent"  
       android:orientation= "vertical"
       tools:context= ".MainActivity" >
     
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="hello" />
     
        <AutoCompleteTextView
            android:id="@+id/autocomplete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </AutoCompleteTextView >
     
    </LinearLayout>
     
    java文件:
    package com.just;
     
    import android.os.Bundle;
    import android.app.Activity;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
     
    public class MainActivity extends Activity {
     
            private static final String[] autostr = new String[] { "a" , "abc" , "abcd" ,
                          "abcde", "abcdef" };
     
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                   super.onCreate(savedInstanceState);
                  setContentView(R.layout. activity_main);
                  ArrayAdapter<?> adapter = new ArrayAdapter<Object>(this,
                               android.R.layout. simple_dropdown_item_1line, autostr );
                  AutoCompleteTextView myautoview = (AutoCompleteTextView) findViewById(R.id.autocomplete );
                  myautoview.setThreshold(3);
                  myautoview.setAdapter(adapter);
     
           }
     
    }
    ps:在使用新窗口的同时一定要是用adapter来实现
  • 相关阅读:
    LeetCode_222.完全二叉树的节点个数
    LeetCode_219.存在重复元素 II
    LeetCode_217.存在重复元素
    LeetCode_215.数组中的第K个最大元素
    LeetCode_21.合并两个有序链表
    LeetCode_206.反转链表
    LeetCode_205.同构字符串
    LeetCode_202.快乐数
    LeetCode_20.有效的括号
    LeetCode_2.两数相加
  • 原文地址:https://www.cnblogs.com/cwr941012/p/4909988.html
Copyright © 2020-2023  润新知