EditText 大众知识

介绍

A text field allows the user to type text into your app. It can be either single line or multi-line. Touching a text field places the cursor and automatically displays the keyboard. In addition to typing, text fields allow for a variety of other activities, such as text selection (cut, copy, paste) and data look-up via auto-completion.
You can add a text field to you layout with the EditText object. You should usually do so in your XML layout with a < EditText > element.javascript

文本区域给用户输入文字提供了方便,它能够是单行的也能够是多行的。触摸一个文本区域获取光标而且自动弹出键盘。除了能够输入文字,文本区域还能够用来执行如文本选中(剪切,复制和粘贴)和经过自动补全实现的数据查询等功能。你能够经过在布局中添加一个EditText对象来实现文本区域,固然也能够在布局文件中添加EditText Tag.
html

这里写图片描述

指定键盘类型

Text fields can have different input types, such as number, date, password, or email address. The type determines what kind of characters are allowed inside the field, and may prompt the virtual keyboard to optimize its layout for frequently used characters.java

You can specify the type of keyboard you want for your EditText object with the android:inputType attribute. For example, if you want the user to input an email address, you should use the textEmailAddress input type:
文本区域有不一样的输入类型,如数字,日期,密码或者邮箱地址。输入类型决定了文本区域内容许输入的字符类型,同时也提示着虚拟键盘给用户展现更加经常使用的字符集合。咱们能够经过使用 android:inputType 属性来明确咱们的输入框想要的键盘类型。例如,若是你想要输入邮箱地址,你可使用textEmailAddress输入类型。android

<EditText
    android:id="@+id/email_address"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/email_hint"
    android:inputType="textEmailAddress" />复制代码
android:inputType Notice
"none" 不弹出键盘
"text" 普通文本键盘
"textEmailAddress" 普通文本键盘,"@"
"textUri" 普通文本键盘,"/"
"number" 数字键盘
"phone" Phone-Style键盘

控制其余行为

android:inputType属性不只能够用来控制显示特定的键盘类型,并且还能够用来明确一些键盘的行为,例如是否大写,自动补全或者是拼写建议等。android:inputType使用按位与的方式,因此能够指定多种行为数组

android:inputType Notice
"textCapSentences" 正常的文本键盘可是每一句话第一个字母大写
"textCapWords" 正常文本键盘可是每个单词的第一个字母大写
"textAutoCorrect" 自动提示拼写错误
"textPassword" 输入的文字都变成点点
"textMultiLine" 容许输入多行文本,能够换行





关于android:inputType属性有不少,上面都只是一些栗子,感受应该很全了,你们能够自行研究。
app


指定键盘行为

除了改变键盘的输入类型,Android容许当用户完成输入指定一个动做,出现的操做指定按钮的回车键和动做,如“搜索”或“发送”键。
例如:ide

<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSearch" />复制代码

键盘会显示出“搜索”按钮,横屏以后不只键盘会出现搜索按钮,右侧还会出现对应的字串
布局



主要是对 android:imeOptions 属性的使用
这么多的行为可供选择,上面只是举一个栗子

这里写图片描述


对键盘中定义的行为的响应

经过android:imeOptions 定义的行为,咱们能够对它定义的行为做出响应.咱们可使用TextView.OnEditorActionListener来进行事件的监听和处理。经过如 IME_ACTION_SEND 或者IME_ACTION_SEARCH等事件ID来针对不一样的事件行为作出不一样的处理。
例如ui

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});复制代码

自定义输入完成后键盘行为的标签

android:imeActionLabel属性就是用来设置这个内容的this

<EditText
    android:id="@+id/launch_codes"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/enter_launch_codes"
    android:inputType="number"
    android:imeActionLabel="启动" />复制代码


自动补全建议

使用AutoCompleteTextView

布局定义

<AutoCompleteTextView
        android:completionThreshold="1"//默认是2,这就是为何默认输入两个字符才能显示出提示信息的缘由
        android:id="@+id/autocomplete_country"
        android:layout_width="match_parent"
        android:text="@android:string/dialog_alert_title"
        android:layout_height="wrap_content" />复制代码

代码实现

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

        // Get a reference to the AutoCompleteTextView in the layout
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
        // Get the string array
        String[] countries = getResources().getStringArray(R.array.countries_array);
        // Create the adapter and set it to the AutoCompleteTextView
        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
        textView.setAdapter(adapter);//设置提示内容
    }复制代码

提示数组

<string-array name="countries_array">
        <item>Afghanistan</item>
        <item>Albania</item>
        <item>Algeria</item>
        <item>American Samoa</item>
        <item>Andorra</item>
        <item>Angola</item>
        <item>Anguilla</item>
        <item>Antarctica</item>
    </string-array>复制代码


备注

内容主要来自于Android官网教程:
developer.android.com/guide/topic…

相关文章
相关标签/搜索