前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >笔记32 | UI系列之EditText和AutoCompleteTextView

笔记32 | UI系列之EditText和AutoCompleteTextView

作者头像
项勇
发布2018-06-19 15:27:14
4360
发布2018-06-19 15:27:14
举报
文章被收录于专栏:项勇项勇

地址

http://blog.csdn.net/xiangyong_1521/article/details/78499706

目录

  • EditText
  • AutoCompleteTextView


1.EditText

简单来说说EditText这个控件,这个就相当于我们平常web开发中的文本输入框,我们如果要使用EditText,可以在布局文件中声明一个这个元素即可,下面就是一个简单的EditText的控件声明:

<EditText 
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="username"/>

对于EditText来说,其最重要的一个属性是 android:inputType,这个属性不仅可以指定键盘的显示类型,还能控制一些其他的操作,具体可以参考android的官方API,其默认属性是 android:inputType="text",也就是普通的键盘框,如果我们设置其属性为以下这些,那么其键盘的类型会有所不同:

<EditText 
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="password"
        android:inputType="textPassword"/>  这个是我们的密码框

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="email"
        android:inputType="textEmailAddress"/>  当设置为textEmailAddress时,键盘会多出来一个 @ 符号

    <EditText
        android:id="@+id/blog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="blog url"
        android:inputType="textUri"/>  设置为textUri时,键盘会多出一个 / 符号

    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="phone"
        android:inputType="phone"/>  设置为phone时,键盘就会变成一个打电话时的键盘

    <EditText
        android:id="@+id/counts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="counts"
        android:inputType="number"/>  设置为number时,键盘上全部都是数字键

对于输入框来说,我们通常都要对其输入的数据进行判断,inputType这个属性不会对我们输入的内容进行校验,如果我们要对输入的内容进行校验,我们需要在Activity里面进行操作 EditText有一个setError的方法,当调用这个方法时,则表示输入的数据不合法,我们来看看官方的API对该方法的解释:

void android.widget.TextView.setError(CharSequence error)

Sets the right-hand compound drawable of the TextView to the "error" icon and sets an error message that will be displayed in a popup when the TextView has focus. The icon and error message will be reset to null when any key events cause changes to the TextView's text. If the error is null, the error message and icon will be cleared.

这个方法会给我们一个错误的小图标以及弹出的一段错误提示信息,当我们的这个EditText控件获得焦点的时候,当我们在文本框中输入了任何的值后,这个icon和message都会消失,例如:


2.AutoCompleteTextView

AutoCompleteTextView这个是一个自动提示内容的文本框,其是EditText的一个子类,

public class AutoCompleteTextView extends EditText

我们通常都是自己定义了一组数据集合,可以是array,可以是list,还可以是网络传过来的数据,这组数据是以下拉菜单的方式根据我们输入的关键字来匹配我们数据集合中满足条件的数据项,通过下拉菜单,我们可以enter来选中我们需要的数据,而为AutoCompleteTextView这个控件提供内容的就是我们的 Adapter ,这个叫做适配器,Adapter这个类的作用就是在我们的Data和我们的View之间架设一座桥梁,我们将我们的数据放置到一个Adapter当中,然后通过指定我们对这些数据的布局方式,再将这个Adapter赋给我们的View。

Adapter是一个接口,其拥有许多的实现类,例如:

android.widget.Adapter

Known Indirect Subclasses:

ArrayAdapter<T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, WrapperListAdapter

我们看到,Android为我们提供了许多的Adapter,这是因为我们的数据可能来自不同的途径,而且对于一些特殊的控件,例如Spinner,我们也要有指定的SpinnerAdapter才行,接下来我们就通过一个例子来实现我们的 AutoCompleteTextView 文本提示功能: XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/llll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <AutoCompleteTextView
            android:id="@+id/autoCompleteTextView"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:completionHint="最近5条记录"
            android:completionThreshold="2" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="搜索" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="清除" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/llll"
        android:text="搜索结果:" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white" >

        <TextView
            android:id="@+id/textv"
            android:textSize="30dp"
            android:textColor="@android:color/black"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

</LinearLayout>

Main

public class MainActivity extends Activity {

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        creatView();  
    }  
    ArrayAdapter<String> adapter; 
    AutoCompleteTextView autoCompleteTextView;
    TextView textv;
    String[] si = {"aaa","aab","aac","aad","aae","aaf","aag"};
    StringBuilder builder;
    private void creatView(){
        textv = (TextView) findViewById(R.id.textv);
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
        builder = new StringBuilder();
        findViewById(R.id.button).setOnClickListener(new OnClickListener() { //搜索 

            @Override
            public void onClick(View v) {
                String s = autoCompleteTextView.getText().toString();
                builder.append(s+",");
                textv.setText(""+builder.toString());
            }
        });
        findViewById(R.id.button2).setOnClickListener(new OnClickListener() { //清除

            @Override
            public void onClick(View v) {
                builder = new StringBuilder();
                textv.setText("没有搜索结果");
            }
        });
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,si);
        autoCompleteTextView.setAdapter(adapter);
    }  
}

我们看到,在Activity里面我们定义了一个ArrayAdapter这个类,其有许多的构造方法,我们来看看我们用的这个:

public ArrayAdapter (Context context, int resource, List<T> objects)

Parameters
context    The current context.
resource    加载界面的布局文件
objects    加载数据

也有可在String.xml中添加数据

<string-array name="name">
        <item>aaa</item>
        <item>aab</item>
        <item>aac</item>
        <item>aad</item>
        <item>aae</item>
        <item>aaf</item>
        <item>aag</item>
    </string-array>

加载

String[] name2 = getResources().getStringArray(R.array.name);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name2);

最后是运行效果


本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-11-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 地址
  • 目录
    • 1.EditText
      • 2.AutoCompleteTextView
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档