问题
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:
tap the EditText widget to get focus, then
use
device(focused=True).clear_text()
to clear the view, or bydevice(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