当前位置: 首页 > news >正文

【Touchinput 】指定输入方法类型(11)

每个文本字段都需要某种类型的文本输入,例如电子邮件地址,电话号码或纯文本。因此,为应用程序中的每个文本字段指定输入类型非常重要,以便系统显示相应的软输入方法(例如屏幕上的键盘)。

除了使用输入法提供的按钮类型之外,还应指定输入方法是否提供拼写建议,大写新句子以及用动作按钮(如“完成”或“ 下一步”)替换回车按钮等行为。本课介绍如何指定这些特征。

指定键盘类型


您应该始终通过向元素添加android:inputType属性来为文本字段声明输入方法<EditText>。

【Touch&input 】指定输入方法类型(11)
图1的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"值,以便文本字段隐藏用户的输入:
【Touch&input 】指定输入方法类型(11)
图2.该textPassword输入类型。

<EditText
    android:id="@+id/password"
    android:hint="@string/password_hint"
    android:inputType="textPassword"
    ... />

有几个可能的值记录在 android:inputType属性中,并且可以组合一些值来指定输入法外观和其他行为。

启用拼写建议和其他行为


该android:inputType属性允许您为输入方法指定各种行为。最重要的是,如果您的文本字段用于基本文本输入(如用于文本消息),则应该使用该"textAutoCorrect"值启用自动拼写校正 。

您可以将不同的行为和输入法样式与该android:inputType属性组合在一起 。例如,下面介绍如何创建一个文本字段来大写句子的第一个单词,并自动更正拼写错误:

<EditText
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType=
        "textCapSentences|textAutoCorrect"
    ... />

【Touch&input 】指定输入方法类型(11)
图3.添加textAutoCorrect 提供了拼写错误的自动纠正。

指定输入法操作


大多数软输入方法在适合当前文本字段的底部角落处提供用户操作按钮。默认情况下,除非您的文本字段允许使用多行文本(例如with ),否则系统会将此按钮用于Next或 Done操作 android:inputType="textMultiLine",在这种情况下,操作按钮是回车符。但是,您可以指定可能更适合您的文本字段的其他操作,例如“ 发送”或“ 去”。

要指定键盘操作按钮,请使用android:imeOptions 具有操作值(例如"actionSend"或) 的属性"actionSearch"。例如:

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

【Touch&input 】指定输入方法类型(11)
图4.发布按钮在您声明时出现 android:imeOptions="actionSend"。

然后,您可以通过TextView.OnEditorActionListener为EditText 元素定义一个按钮来监听操作按钮 。在您的侦听器中,响应EditorInfo类中定义的相应IME操作ID ,例如 IME_ACTION_SEND。例如:

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提供文本建议。有几种适配器可用,具体取决于数据来自何处,例如来自数据库或阵列。
【Touch&input 】指定输入方法类型(11)
图5.AutoCompleteTextView带有文本建议的 示例

以下过程描述如何设置一个AutoCompleteTextView从数组中提供建议的方法,使用ArrayAdapter:

添加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资源文件(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>

在你的Activity或者Fragment,使用下面的代码来指定提供建议的适配器:

// 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字符串,将字符串数组中的每个项绑定到 布局TextView中存在的项simple_list_item_1(这是Android提供的布局,为列表中的文本提供标准外观)。

AutoCompleteTextView通过调用 将适配器分配给setAdapter()。

Lastest Update:2018.04.25

联系我

QQ:94297366
微信打赏:https://pan.baidu.com/s/1dSBXk3eFZu3mAMkw3xu9KQ

公众号推荐:

【Touch&input 】指定输入方法类型(11)

转载于:https://blog.51cto.com/4789781/2125387

相关文章:

  • iOS中父类readonly属性修改
  • μCOS-II系统之事件(event)的使用规则及MUTEX实例
  • 之所以一无所成,并不是我们不够努力
  • [转]Ubuntu16 压缩解压文件命令
  • 数组全部整理
  • corosync+pacemaker配置高可用集群(需要额外安装crm工具)
  • Vmvare 虚拟机固定IP
  • 用Linux shell 计算两个时间差
  • Git 打补丁流程
  • Vagrant 基础全面解析
  • Elasticsearch重启前禁止分片移动的方法
  • Redis从入门到精通:初级篇(转)
  • 2017 Node.js 开发框架比较
  • systemd-journald日志持久化的操作方法
  • Azure系列2.1.7 —— BlobRequestOptions
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • eclipse的离线汉化
  • java 多线程基础, 我觉得还是有必要看看的
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • java中的hashCode
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • nfs客户端进程变D,延伸linux的lock
  • php面试题 汇集2
  • spark本地环境的搭建到运行第一个spark程序
  • TypeScript实现数据结构(一)栈,队列,链表
  • windows下如何用phpstorm同步测试服务器
  • 二维平面内的碰撞检测【一】
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 前端存储 - localStorage
  • 如何优雅地使用 Sublime Text
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • 进程与线程(三)——进程/线程间通信
  • ​ 无限可能性的探索:Amazon Lightsail轻量应用服务器引领数字化时代创新发展
  • ​Base64转换成图片,android studio build乱码,找不到okio.ByteString接腾讯人脸识别
  • ​比特币大跌的 2 个原因
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • ${ }的特别功能
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (强烈推荐)移动端音视频从零到上手(上)
  • (三分钟)速览传统边缘检测算子
  • (学习日记)2024.01.19
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • ./和../以及/和~之间的区别
  • .net core 6 集成和使用 mongodb
  • .Net MVC4 上传大文件,并保存表单
  • .NET Windows:删除文件夹后立即判断,有可能依然存在
  • .NET设计模式(8):适配器模式(Adapter Pattern)
  • .Net通用分页类(存储过程分页版,可以选择页码的显示样式,且有中英选择)
  • /bin/bash^M: bad interpreter: No such file or directory
  • ?php echo $logosrc[0];?,如何在一行中显示logo和标题?
  • @for /l %i in (1,1,10) do md %i 批处理自动建立目录
  • @GetMapping和@RequestMapping的区别
  • @RequestBody与@ModelAttribute