Android x Kotlin : EditText与软键盘常见设定

简介

editText有些常用设定,有时候会不小心忽略掉。虽然有些不是必备,但使用者体验的优化还是很重要ㄉ,继续看下去。

图片来源:台湾漫画基地

<EditText
  android:id="@+id/et_phone_verify_code"
  style="@style/EditTextStyle"
  android:layout_width="match_parent"
  android:layout_height="36dp"
  android:layout_marginTop="6dp"
  android:background="@color/colorf8f8f8"
  android:focusableInTouchMode="true"
  android:hint="@string/phone_verify_code"
  android:imeOptions="actionDone"
  android:inputType="number"
  android:paddingStart="16dp"
  android:singleLine="true"/>

1. singleLine与InputType

记得设定singleLineInputType,以及设定每个editText的软键盘右下角的按键,一般预设会显示enter,想改的话可以利用edittext的imeOptions属性依需求改成不同功能键。

  • android:imeOptions="actionSearch" 文字为搜寻
  • android:imeOptions="actionGo" 文字为开始
  • android:imeOptions="actionSend" 文字为传送
  • android:imeOptions="actionDone" 文字为Enter键
  • ...................

2. focusableInTouchMode 属性

还不太懂,可参考这篇

3. 密码栏位的edittext:

设定密码显示

et.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

设定密码隐藏:

et.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD

因为上述调整完後,edittext的游标会回到最前面,所以要同时设定这一行,把游标保持在最末端

et.setSelection(et.text.length)

4. 收起软键盘

fun hideKeyboard(view: View, nextFoocusView: View = view.rootView) {
    val imm = view.context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(view.windowToken, 0)
    view.clearFocus()
    nextFoocusView.requestFocus()
}

5.软键盘右下角的完成钮

我们要监听使用者按下软键盘的控制键,譬如软键盘右下角的完成钮

et.setOnEditorActionListener { v, actionId, event ->
    if(actionId == EditorInfo.IME_ACTION_DONE){
            ....
    }
    false
}

可以与imeOption属性做搭配!!!!
imeOptions=”actionUnspecified” –> EditorInfo.IME_ACTION_UNSPECIFIED
imeOptions=”actionNone” –> EditorInfo.IME_ACTION_NONE
imeOptions=”actionGo” –> EditorInfo.IME_ACTION_GO
imeOptions=”actionSearch” –> EditorInfo.IME_ACTION_SEARCH
imeOptions=”actionSend” –> EditorInfo.IME_ACTION_SEND
imeOptions=”actionNext” –> EditorInfo.IME_ACTION_NEXT
imeOptions=”actionDone” –> EditorInfo.IME_ACTION_DONE

30天过去,铁人赛与案子的压力算是帮我分担了很多容易胡思乱想的时间,但即使一个月过去,思考中断的间隙总还是会被那些思绪趁虚而入。
曾大言不惭我们不会被时间冲淡,现在却只希望它冲淡一切。

事情发生後连带产生的自我怀疑也成为新的负担,工作上一有不顺就常常质疑自己,觉得是不是不够努力,不够聪明,是不是什麽都做不好。
这一个月相当难熬,但还是过来了,谢谢自己,也谢谢身边那些带来温暖的人。2020铁人赛完赛,该吃海港城了八!!


<<:  强型闯入DenoLand[29] - 去标签密技

>>:  总结

Vue.js 从零开始:emit 元件的沟通

上一篇已经学会如何把外部元件的资料传到内部元件,如果是内部传到外部,就需要透过emit来达成。 Pr...

25 把卡片摆一摆

来把卡摆上去吧 我们先来做卡的外型 放在 lib/card_web/component.ex 里面 ...

day8 kotlin coroutine的 runBlocking, withContext

runBlocking 中文用户如我们? 直译一下就是,跑一个塞住 恩,三小? 他真正的意思是,执行...

(),[] -- 列表与元组

序列 索引 python比较神奇,可以从正面数或反着数,每一个位置都有自己的下标 切片 假设现在有一...

资安学习路上-picoCTF 解题(Reverse)2

4.speeds and feeds Google後发现CNC 的language是"G-...