在按钮上调用键盘的删除/退格操作?

问题描述 投票:-1回答:1

有没有一种方法可以将键盘上的退格键放到按钮上,或者我必须自定义它?

android android-edittext keyboard android-softkeyboard keyboard-events
1个回答
0
投票

我们可以使用Instrumentation类通过代码模拟Keyevent

kotlin

val instrumentation = Instrumentation()
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DEL)

我刚刚对其进行了测试,并在我的仿真器(API 29)上工作

注意: Instrumentation不能在Instrumentation中调用>


所以您可以在下面这样做

main-thread

Kotlin

thread { Instrumentation() instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DEL) {

Java

KeyEvent.KEYCODE_DEL

= BackSpace

KeyEvent.KEYCODE_FORWARD_DEL

= 删除

更新示例

让我为您展示片段

new Thread()
{
    ..
    new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DEL);
    ..
}.start();

btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(){ @Override public void run() { Instrumentation instrumentation = new Instrumentation(); instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DEL); } }.start(); } });

© www.soinside.com 2019 - 2024. All rights reserved.