Continuously delete contents of EditText on button hold Android [duplicate]

荒凉一梦 提交于 2019-12-06 13:46:35

问题


How do I delete one by one the current text of edit text when I press a button and continuously delete the text one by one when I hold the press of button. Similar to what a backspace button does.

Sending a backspace event won't work on my needs because it will only work if the keyboard is active. Im not using any keyboards.


回答1:


You have to use the Timer for this.It continuously checks the button state pressed or not. Like:

private Timer _timer;

final Button _button = (Button) findViewById(R.id.button1);
final EditText _editText = (EditText) findViewById(R.id.editText1);
_timer = new Timer();
TimerTask _task = new TimerTask() {

@Override
public void run() {
runOnUiThread(new Runnable() {

 @Override
 public void run() {
    Editable _editable = _editText.getText();
    int length = _editable.length();
    _editText.setSelection(length);
    if (_button.isPressed() && length > 0) {
        _editable.delete(length - 1, length);
        }

    }
 });
}
};
_timer.schedule(_task, 0, 60);

Just cancel the Timer in onDestroy() method, Like:

_timer.cancel();



回答2:


You can send a BACKSPACEKEY EVENT to your EditText.

Here is the sample for what you want Send backspace key event to edit text.

Edit

Manually you can do like this

  • Get the String from edittext.

  • Delete the char at last index. For this you can use method subString of String class.

Recursively do this process until your String becomes empty.



来源:https://stackoverflow.com/questions/20987540/continuously-delete-contents-of-edittext-on-button-hold-android

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