源码教程 2025年06月7日
0 收藏 0 点赞 423 浏览 2807 个字
摘要 :

在使用百度或者 Google 搜索信息时,只需要在搜索框中输入几个关键字,就会有很多相关的信息以列表形式被列举出来供用户选择,这种效果在 Android SDK 中可以……

使用百度或者 Google 搜索信息时,只需要在搜索框中输入几个关键字,就会有很多相关的信息以列表形式被列举出来供用户选择,这种效果在 Android SDK 中可以通过 AutoCompleteTextView 来实现。

下面用一个简单的实例讲解 AutoCompleteTextView 的使用方法。在工程 WidgetDemo 的布局文件 main.xml 中添加一个 Button,用以启动 AutoCompleteTextViewActivity。

在 main.xml 中添加代码如下:

  1. <Button

  2.     android:id="@+id/button5"

  3.     android:layout_width="wrap_content"

  4.     android:layout_height="wrap_content"

  5.     android:text="AutoCompleteTextViewDemo"/>

单击 Button 并启动 AutoCompleteTextViewActivity 的代码如下:

  1. Button autobtn = (Button)this.findViewById(R.id.button5);

  2. autobtn.setOnClickListener(new OnClickListener(){

  3. @Override

  4.   public void onClick(View v){

  5.        Intent intent = new Intent(WidgetDemoActivity.this, AutoCompleteTextViewActivity.class);

  6.        startActivity(intent);

  7.      }

  8. });

同时在 AndroidManifest.xml 文件中声明该 Activity:

<activity android:name=".AutoCompleteTextViewActivity"></activity>

AutoCompleteTextViewActivity 的运行效果如图 1 所示。

Android自动完成文本框控件AutoCompleteTextView使用

图 1  AutoCompleteTextViewActivity 的运行效果

AutoCompleteTextViewActivity 使用的布局文件为 autocompletetextview.xml,其具体内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical">

  6. <TextView

  7. android:layout_width="fill_parent"

  8. android:layout_height="wrap_content"

  9. android:text="AutoCompleteTextView演示:" />

  10. <AutoCompleteTextView

  11. android:id="@+id/autoCompleteTextView1"

  12. android:layout_width="match_parent"

  13. android:layout_height="wrap_content"

  14. android:text="" />

  15. <requestFocus />

  16. </LinearLayout>

AutoCompleteTextViewActivity.java 的代码如下:

package introduction.android.widgetdemo;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

public class MainActivity extends Activity {
    private AutoCompleteTextView textView;
    private static final String[] autotext = new String[]{"张三","张无忌","张三丰"};
   
    @Override
    public void onCreat(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autocompletetextview);
        textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        /*new ArrayAdapterd 对象将 autotext字符串数组传入*/
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this,android.R.layout.simple_dropdown_item_1line,autotext);
        /*将ArrayAdapter添加到AutoCompleteTextView中*/
        textView.setAdapter(adapter);
    }
}

AutoCompleteTextViewActivity 中为可自动补全的内容建立对应字符串数组 autotext,将该数组关联到 ArrayAdapter 中,然后将 ArrayAdapter 与 AutoCompleteTextView 相关联,进而实现自动完成文本功能。

AutoCompleteTextView 提供一系列属性对显示效果进行设置,分别说明如下。

  • completionThreshold:它的值决定了你在 AutoCompleteTextView 中至少输入几个字符,才会具有自动提示的功能。另外,默认最多提示 20 条。

  • dropDownAnchor:它的值是一个 View 的 ID,指定后,AutoCompleteTextView 会在这个 View 下弹出自动提示。

  • dropDownSelector:应该是设置自动提示的背景色之类的,没有尝试过,有待进一步考证。

  • dropDownWidth:设置自动提示列表的宽度。

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/968.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

701
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

715
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

307
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,039
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

835
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

723
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号