Clear Edit Text - adb

柔情痞子 提交于 2019-12-23 10:19:11

问题


How to clear focused Edit text using shell command.

I tried

adb shell input keyevent KEYCODE_CLEAR // Not worked 
adb shell input keyevent KEYCODE_DEL // Delete only one char
adb shell input keyevent KEYCODE_FORWARD_DEL // Not worked

With this I am only able to delete upto One character only, Is there any way I can delete/clear the focused Edit text.


回答1:


This works for me:

function clear_input() {
    adb shell input keyevent KEYCODE_MOVE_END
    adb shell input keyevent --longpress $(printf 'KEYCODE_DEL %.0s' {1..250})
}

Then:

clear_input



回答2:


The only way that I have found so far is to get the proper coordinates to use input swipe x y x y duration to simulate a long press. This will then highlight all the text in the EditText field. You can then send the keys you want to replace what was there.

I just wish that adb shell input keyevent KEYCODE_CLEAR would clear all the text in the field. That would make things so much easier, if someone can find a better way that would be great.




回答3:


If you use uiautomator(https://github.com/xiaocong/uiautomator), you could do this by:

  1. tap the EditText widget to get focus, then

  2. use device(focused=True).clear_text() to clear the view, or by device(focused=True).set_text("new text") to set a new text.




回答4:


probably next solution looks like a hack but it's worked:

        mEditText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (KeyEvent.KEYCODE_CLEAR == keyCode) {
                    mEditText.setText("");
                }
                return true;
            }
        });


来源:https://stackoverflow.com/questions/32433303/clear-edit-text-adb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!