每个文本字段都需要特定类型的文本输入,例如电子邮件地址、电话号码或纯文本。您必须为应用中的每个文本字段指定输入类型,以便系统显示适当的软输入法,例如屏幕键盘。
除了输入法可用的按钮类型外,您还可以指定某些行为,例如输入法是否提供拼写建议、是否自动大写新句子的首字母,以及是否将回车按钮替换为完成或下一项等操作按钮。本页面介绍了如何指定这些特性。
指定键盘类型
务必通过在 <EditText> 元素中添加 android:inputType 属性,为您的文本字段声明输入法。
phone 输入类型。例如,如果您需要用于输入电话号码的输入法,请使用 "phone" 值
<EditText android:id="@+id/phone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/phone_hint" android:inputType="phone" />
textPassword 输入类型。如果文本字段用于输入密码,请使用 "textPassword" 值,这样文本字段会隐藏用户的输入内容
<EditText android:id="@+id/password" android:hint="@string/password_hint" android:inputType="textPassword" ... />
android:inputType 属性记录了多种可能的值,您可以组合其中一些值来指定输入法的外观及附加行为。
启用拼写建议和其他行为
textAutoCorrect 可为拼写错误提供自动更正功能。android:inputType 属性允许您为输入法指定多种行为。最重要的是,如果您的文本字段旨在进行基本的文本输入(例如用于发送短信),请使用 "textAutoCorrect" 值来启用自动拼写更正。
您可以使用 android:inputType 属性组合不同的行为和输入法样式。例如,以下代码展示了如何创建一个自动大写句子首字母并自动更正拼写错误的文本字段
<EditText android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType= "textCapSentences|textAutoCorrect" ... />
指定输入法操作
大多数软输入法会在底角提供一个适用于当前文本字段的用户操作按钮。默认情况下,系统会将此按钮用于下一项或完成操作,除非您的文本字段支持多行文本(例如使用 android:inputType="textMultiLine"),在这种情况下,操作按钮为回车键。不过,您可以指定更适合您的文本字段的其他操作,例如发送或搜索。
要指定键盘操作按钮,请使用 android:imeOptions 属性并配合操作值(如 "actionSend" 或 "actionSearch")。例如
android:imeOptions="actionSend" 时,会出现发送按钮。<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" />
然后,您可以通过为 EditText 元素定义 TextView.OnEditorActionListener 来监听操作按钮的点击。在您的监听器中,响应 EditorInfo 类中定义的适当 IME 操作 ID(例如 IME_ACTION_SEND),如下例所示
Kotlin
findViewById<EditText>(R.id.search).setOnEditorActionListener { v, actionId, event -> return@setOnEditorActionListener when (actionId) { EditorInfo.IME_ACTION_SEND -> { sendMessage() true } else -> false } }
Java
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; } });
提供自动完成建议
如果您希望在用户输入时提供建议,可以使用 EditText 的子类 AutoCompleteTextView。要实现自动完成功能,必须指定一个提供文本建议的 Adapter。根据数据来源(例如数据库或数组),有多种适配器可供选择。
AutoCompleteTextView 示例。以下步骤说明了如何设置一个使用 ArrayAdapter 从数组中提供建议的 AutoCompleteTextView
- 将
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" />
- 定义包含所有文本建议的数组。例如,这是一个国家名称数组
<?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>
- 在您的
Activity或Fragment中,使用以下代码指定提供建议的适配器Kotlin
// Get a reference to the AutoCompleteTextView in the layout. val textView = findViewById(R.id.autocomplete_country) as AutoCompleteTextView // Get the string array. val countries: Array<out String> = resources.getStringArray(R.array.countries_array) // Create the adapter and set it to the AutoCompleteTextView. ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries).also { adapter -> textView.setAdapter(adapter) }
Java
// 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);
在上述示例中,初始化了一个新的
ArrayAdapter,将countries_array字符串数组中的每一项绑定到simple_list_item_1布局中现有的TextView上。这是 Android 提供的一种布局,为列表中的文本提供了标准外观。 - 通过调用
setAdapter()将适配器分配给AutoCompleteTextView。