在按住按钮的Android上连续删除EditText的内容 [英] Continuously delete contents of EditText on button hold Android

查看:685
本文介绍了在按住按钮的Android上连续删除EditText的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在按下按钮时一一删除编辑文本的当前文本,并在按住按钮时一一连续删除文本.类似于后退按钮的功能.

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.

推荐答案

为此您必须使用计时器.它会连续检查按钮状态是否为按下状态.喜欢:

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);

只需在onDestroy()方法中取消Timer,例如:

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

_timer.cancel();

这篇关于在按住按钮的Android上连续删除EditText的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆