自动文本框提示

activity_main.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" >

    <!--

      默认输2个字符才能有提示

      completionThreshold表示只输入1个字符后,就有提示

      requestFocus表示界面展开时焦点直接在第二个文本框

    -->

    <AutoCompleteTextView

        android:id="@+id/myTextView01"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:completionThreshold="1" />

    <MultiAutoCompleteTextView

        android:id="@+id/myTextView02"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:completionThreshold="1">

        <requestFocus />

    </MultiAutoCompleteTextView>

</LinearLayout>

代码实现
public class MainActivity extends Activity {

  private AutoCompleteTextView myTextView01;
  private MultiAutoCompleteTextView myTextView02;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myTextView01 = (AutoCompleteTextView) findViewById(R.id.myTextView01);
    myTextView02 = (MultiAutoCompleteTextView) findViewById(R.id.myTextView02);

    String[] str={"xiaohe","xiaowang","xiaoli","zhanghe","zhangmin","zhaojun","lihe","daming"};
    /*
    * 创建适配器
    * 参数一:上下文
    * 参数二:提示下位框的样式,不喜欢可以换android.R.layout.*
    * 参数三:下拉框中备选的内容
    */
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(
    this,
    android.R.layout.simple_dropdown_item_1line,
    str);

    //将Adapter设置到AutoCompleteTextView中
    myTextView01.setAdapter(adapter);

    myTextView02.setAdapter(adapter);
    //以","作为分隔符
    myTextView02.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
  }
}

转载于:https://www.cnblogs.com/konekou/p/7699173.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值