Android EditText键盘属性
定制键盘的按钮属性需要用到一个类EditorInfo类;
public class EditorInfo
extends Object
implements Parcelable, InputType
这个类中定义类常用的一些键盘属性,如下:
类型 | 类型-值 | 描述 |
---|---|---|
int | IME_ACTION_DONE | Bits of IME_MASK_ACTION: the action key performs a “done” operation, typically meaning there is nothing more to input and the IME will be closed. |
int | IME_ACTION_GO | Bits of IME_MASK_ACTION: the action key performs a “go” operation to take the user to the target of the text they typed. |
int | IME_ACTION_NEXT | Bits of IME_MASK_ACTION: the action key performs a “next” operation, taking the user to the next field that will accept text. |
int | IME_ACTION_NONE | Bits of IME_MASK_ACTION: there is no available action. |
int | IME_ACTION_PREVIOUS | Bits of IME_MASK_ACTION: Like IME_ACTION_NEXT, but for moving to the previous field. |
int | IME_ACTION_SEARCH | Bits of IME_MASK_ACTION: the action key performs a “search” operation, taking the user to the results of searching for the text they have typed (in whatever context is appropriate). |
int | IME_ACTION_SEND | Bits of IME_MASK_ACTION: the action key performs a “send” operation, delivering the text to its target. |
int | IME_ACTION_UNSPECIFIED | Bits of IME_MASK_ACTION: no specific action has been associated with this editor, let the editor come up with its own if it can. |
int | IME_FLAG_FORCE_ASCII | Flag of imeOptions: used to request an IME that is capable of inputting ASCII characters. |
int | IME_FLAG_NAVIGATE_NEXT | Flag of imeOptions: used to specify that there is something interesting that a forward navigation can focus on. |
int | IME_FLAG_NAVIGATE_PREVIOUS | Flag of imeOptions: like IME_FLAG_NAVIGATE_NEXT, but specifies there is something interesting that a backward navigation can focus on. |
int | IME_FLAG_NO_ACCESSORY_ACTION | Flag of imeOptions: used in conjunction with one of the actions masked by IME_MASK_ACTION, this indicates that the action should not be available as an accessory button on the right of the extracted text when the input method is full-screen. |
int | IME_FLAG_NO_ENTER_ACTION | Flag of imeOptions: used in conjunction with one of the actions masked by IME_MASK_ACTION. |
int | IME_FLAG_NO_EXTRACT_UI | Flag of imeOptions: used to specify that the IME does not need to show its extracted text UI. |
int | IME_FLAG_NO_FULLSCREEN | Flag of imeOptions: used to request that the IME never go into fullscreen mode. |
int | IME_MASK_ACTION | Set of bits in imeOptions that provide alternative actions associated with the “enter” key. |
int | IME_NULL | Generic unspecified type for imeOptions. |
使用代码如下:
布局文件 activity_main.xml
<EditText
android:id="@+id/editUserName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入名称"
<!-- 设置为搜索样式,你也可以用上面的设定那些属性值 -->
android:imeOptions="actionSearch"
android:singleLine="true"
android:textSize="12sp" />
实现代码:
MainActivity.kt
class MainActivity: AppCompatActivity(), TextView.OnEditorActionListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editUserName.setOnEditorActionListener(this)
}
override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
when (actionId) {
EditorInfo.IME_ACTION_SEARCH -> toast("这里是你要具体执行的业务逻辑")
else -> {
}
}
return true
}
}
Tips:
如果细心的话,你还会发现2个关于自定义键盘的属性:
<!-- 自定义搜索文案 -->
android:imeActionLabel="Sign in"
<!-- 自定义搜索ID,需要注意在res/value/menus.xml定义需要的ID,
也可以在其它你定义的ids.xml中也可以 -->
android:imeActionId="@integer/ime_menu_sign_in"
修改相应的页面代码逻辑即可:
override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
when (actionId) {
EditorInfo.IME_ACTION_SEARCH -> toast("这里是你要具体执行的业务逻辑")
R.id.ime_menu_sign_in -> {
//what you want to do,eg:log,toast, and so on
}
else -> {
}
}
return true
}