Text Fields(文本框)

来源:互联网 发布:大数据征信查询入口app 编辑:程序博客网 时间:2024/04/30 15:48

文本框

文本框允许用户在应用中输入文本。可以单行输入也可以多行输入。点击文本框会获得焦点并显示键盘。出了输入操作之外,文本框还支持其他操作,例如文本选择(剪切,复制,粘贴)和数据的自动完成。

你可以使用 EditText 对象在布局中添加一个文本框。不过通常你应该在XML布局中使用<EditText>元素。

指定键盘类型



图1. 默认text输入类型。


图2. textEmailAddress输入类型。


图3. phone输入类型。

文本框有着不同的输入类型,例如数字,日期,密码或电子邮件地址。这些类型确定文本框可以输入什么类型的字符,并且可能会为经常使用的字符弹出虚拟键盘来优化布局。

你可以在 EditText 对象上使用 android:inputType 属性来指定键盘类型。例如,如果你想要求用户输入电子邮件地址,你应该使用textEmailAddress输入类型:

<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 的一些常用值:

"text"
标准文本键盘。
"textEmailAddress"
带有@字符的标准文本键盘。
"textUri"
带有/字符的标准文本键盘。
"number"
基本数字键盘。
"phone"
电话样式键盘。

控制其他行为

android:inputType 还可以让你指定某些键盘行为,例如是否使所有新单词的首字母大写,或使用自动完成和拼写建议功能。

android:inputType 的属性还可以按位组合,所以你可以同时指定键盘布局和一个或多个行为。

这些是定义键盘行为的一些常用输入类型的值:

"textCapSentences"
大写每个新语句首字母的标准文本键盘。
"textCapWords"
大写每个单词的标准文本键盘。适用于标题或人名。
"textAutoCorrect"
可以纠正一般拼写错误单词的标准文本键盘。
"textPassword"
标准文本键盘,但是输入的字符返回的结果是点。
"textMultiLine"
允许用户输入包含换行符(回车)的长文本串的标准文本键盘。

例如,下面向你介绍如何实现可以输入邮政地址,每个单词大写和禁用文本建议的文本框:

<EditText    android:id="@+id/postal_address"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:hint="@string/postal_address_hint"    android:inputType="textPostalAddress|                       textCapWords|                       textNoSuggestions" />

所有行为都在 android:inputType 文档中被列出。

指定键盘操作



图4. 如果你声明了android:imeOptions="actionSend",那么这个键盘就包含发送操作。

除了改变键盘的输入类型,Android还允许你在用户完成输入时指定将要执行的操作。这个操作可以替换回车键位置显示的按钮和操作,比如“搜索”和“发送”。

你可以通过设置 android:imeOptions 属性指定操作。例如,这里介绍如何指定发送行为:

<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="actionSend" />

如果你没有明确指明输入操作,那么系统会试着确定后面是否有 android:focusable 属性输入框。如果后面发现focusable属性输入框,系统会为当前 EditText 应用"actionNext"操作以便用户可以选择下一步来移动到下一个输入框。如果后面没有focusable属性输入框,系统会应用"actionDone"操作。你可以通过设置 android:imeOptions 属性为诸如"actionSend""actionSearch"或默认行为"actionNone"来重写操作。

响应操作按钮事件

如果你已经使用 android:imeOptions 属性(例如"actionSend")为输入法指定了键盘的操作,你就可以使用 TextView.OnEditorActionListener 监听特定的操作事件。TextView.OnEditorActionListener 接口提供一个名为 onEditorAction() 的回调方法,它通过诸如 IME_ACTION_SEND 或 IME_ACTION_SEARCH 等操作ID调用相关的操作类型方法。

例如,这里介绍如何监听用户点击键盘上的发送按钮:

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 属性来定制这个按钮的文本:

<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="@string/launch" />

图5. 使用 android:imeActionLabel 的自定义操作标签。

添加其他键盘标志


你除了可以使用 android:imeOptions 属性指定操作外,还可以通过添加附加标志指定其他的键盘行为。在 android:imeOptions 文档里连同操作列出了所有可用的标志。

例如,图5展示的是系统在手机横向时如何启用全屏文本框(或屏幕空间在其他方面被约束使用)。你可以如图6所示在 android:imeOptions 属性中设置flagNoExtractUi来禁用全屏输入模式。

图6. 使用android:imeOptions="flagNoExtractUi"后全屏文本模式不可用(“提取模式”)。

提供自动完成建议


如果你想在用户输入时提供建议,那么你可以使用 EditText 的名为 AutoCompleteTextView 的子类达到效果。你必须指定一个能提供文本建议的 Adapter 来实现自动完成功能。有许多种可用的适配器,你可以根据数据来源选择使用,例如来自数据库或数组。


图7. 带文本建议的 AutoCompleteTextView 事例。

下面的步骤介绍如何设置 AutoCompleteTextView 并使用 ArrayAdapter 从数组提供建议:

  1. 在布局中添加 AutoCompleteTextView。这是个只有文本框的布局:
    <?xml version="1.0" encoding="utf-8"?><AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/autocomplete_country"    android:layout_width="fill_parent"    android:layout_height="wrap_content" />
  2. 定义包含文本建议的数组。例如,这是个定义在XML资源文件(res/values/strings.xml)中国家名的数组:
    <?xml version="1.0" encoding="utf-8"?><resources>    <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></resources>
  3. 在 Activity 或 Fragment 中,使用下面的代码来指定提供建议的适配器:
    // Get a reference to the AutoCompleteTextView in the layoutAutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);// Get the string arrayString[] 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);

    在代码中,实例化一个新的 ArrayAdapter 把COUNTRIES字符数组中的每一项绑定到simple_list_item_1布局中的TextView上(这是由Android提供的支持列表中文本的标准外观的布局)

    然后调用 setAdapter() 把适配器分配给 AutoCompleteTextView。

2 0
原创粉丝点击