Android 自动补齐文本框AutoCompleteTextView的使用

博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,
👉点击跳转到网站

首先是布局文件activity_auto_complete.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".AutoCompleteActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="爱好:"
            android:textSize="25sp" />

        <AutoCompleteTextView
            android:id="@+id/auto_tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:completionThreshold="1"
            android:hint="请输入您的爱好" />
    </LinearLayout>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="获取文本框中的值"
        android:textSize="20sp" />

</LinearLayout>

之后在res/values目录下创建array.xml资源文件

<resources>
    <string-array name="hobby">
        <item>读书</item>
        <item>跑步</item>
        <item>唱歌</item>
        <item>旅游</item>
        <item>写代码</item>
        <item>写字</item>
        <item>篮球</item>
        <item>乒乓球</item>
    </string-array>
</resources>

之后在AutoCompleteActivity文件中通过系统提供的数组适配器ArrayAdapter,显示数据。

public class AutoCompleteActivity extends AppCompatActivity {
    private AutoCompleteTextView auto_tv1;
    private ArrayAdapter arrayAdapter;
    private String[] data = new String[]{"读书", "跑步", "唱歌", "旅游", "写代码", "篮球", "乒乓球"};
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_complete);
        auto_tv1 = findViewById(R.id.auto_tv1);
        btn = findViewById(R.id.btn);


//        arrayAdapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item,data );
        arrayAdapter = ArrayAdapter.createFromResource(this,R.array.hobby,R.layout.support_simple_spinner_dropdown_item);
        auto_tv1.setAdapter(arrayAdapter);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(AutoCompleteActivity.this, auto_tv1.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

效果图显示:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值