Android 键盘属性定制及说明

Android EditText键盘属性

定制键盘的按钮属性需要用到一个类EditorInfo类;
public class EditorInfo
extends Object
implements Parcelable, InputType
这个类中定义类常用的一些键盘属性,如下:

类型类型-值描述
intIME_ACTION_DONEBits 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.
intIME_ACTION_GOBits of IME_MASK_ACTION: the action key performs a “go” operation to take the user to the target of the text they typed.
intIME_ACTION_NEXTBits of IME_MASK_ACTION: the action key performs a “next” operation, taking the user to the next field that will accept text.
intIME_ACTION_NONEBits of IME_MASK_ACTION: there is no available action.
intIME_ACTION_PREVIOUSBits of IME_MASK_ACTION: Like IME_ACTION_NEXT, but for moving to the previous field.
intIME_ACTION_SEARCHBits 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).
intIME_ACTION_SENDBits of IME_MASK_ACTION: the action key performs a “send” operation, delivering the text to its target.
intIME_ACTION_UNSPECIFIEDBits of IME_MASK_ACTION: no specific action has been associated with this editor, let the editor come up with its own if it can.
intIME_FLAG_FORCE_ASCIIFlag of imeOptions: used to request an IME that is capable of inputting ASCII characters.
intIME_FLAG_NAVIGATE_NEXTFlag of imeOptions: used to specify that there is something interesting that a forward navigation can focus on.
intIME_FLAG_NAVIGATE_PREVIOUSFlag of imeOptions: like IME_FLAG_NAVIGATE_NEXT, but specifies there is something interesting that a backward navigation can focus on.
intIME_FLAG_NO_ACCESSORY_ACTIONFlag 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.
intIME_FLAG_NO_ENTER_ACTIONFlag of imeOptions: used in conjunction with one of the actions masked by IME_MASK_ACTION.
intIME_FLAG_NO_EXTRACT_UIFlag of imeOptions: used to specify that the IME does not need to show its extracted text UI.
intIME_FLAG_NO_FULLSCREENFlag of imeOptions: used to request that the IME never go into fullscreen mode.
intIME_MASK_ACTIONSet of bits in imeOptions that provide alternative actions associated with the “enter” key.
intIME_NULLGeneric 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
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值