Android Java, 每0.25秒通过 setOnTouchListener 从字符串中删除最后1个字符。

huangapple go评论84阅读模式
英文:

Android Java, setOnTouchListener delete 1 last character from string every 0.25s

问题

清除按钮:

clear.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (operation.length() != 0) {
            String s = operation.getText().toString();
            s = s.substring(0, operation.length() - 1);
            operation.setText(s);
        }
    }
});

长按触发想法:

clear.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
});
英文:

I am making calculator on java. I want to make a button, which corresponds to delete the last character.<br>
If you press on it for 1 second, it starts to delete 1 character every 0.25s form EditText string.<br>

Clear button:

clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(operation.length() != 0){
                String s=operation.getText().toString();
                s = s.substring(0,operation.length()-1);
                operation.setText(s);}
        }
    });

Long touch idea:

clear.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

答案1

得分: 1

你可以使用ScheduledExecutorService按照设定的速率和初始延迟来调度Runnable。以下是我如何实现的:

// 为ExecutorService和ScheduledFuture创建成员变量
private ScheduledExecutorService mExecutor;
private ScheduledFuture<?> mFuture;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    final EditText editText = findViewById(R.id.textView);
    final Button button = findViewById(R.id.button2);

    // 设置ExecutorService
    mExecutor = Executors.newSingleThreadScheduledExecutor();

    final Runnable deleteRunnable = new Runnable() {
        @Override
        public void run() {
            // 我们将使用视图的post()方法
            // 以确保我们从正确的线程更新它
            editText.post(new Runnable() {
                @Override
                public void run() {
                    String textValue = editText.getText().toString();

                    if (textValue.length() > 0) {
                        // 删除最后一个字符
                        textValue = textValue.substring(0, textValue.length() - 1);

                        Log.d("MY_LOG_TAG", textValue);
                        editText.setText(textValue);
                    }

                    // (可选)
                    // 保持光标在文本末尾
                    editText.setSelection(textValue.length());
                }
            });
        }
    };

    button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    Log.d("MY_LOG_TAG", "按下动作");

                    // 使用初始延迟1000毫秒和速率250毫秒调度可运行项
                    mFuture = mExecutor.scheduleAtFixedRate(deleteRunnable, 1000, 250, TimeUnit.MILLISECONDS);
                    break;

                case MotionEvent.ACTION_UP:
                    Log.d("MY_LOG_TAG", "抬起动作");

                    // 当触发Up事件时取消已调度的可运行项
                    if (mFuture != null) {
                        mFuture.cancel(false);
                    }
                    break;
            }
            return false;
        }
    });
}

@Override
protected void onDestroy() {
    // 关闭Executor Service
    mExecutor.shutdown();

    super.onDestroy();
}
英文:

You can use ScheduledExecutorService to schedule Runnables at a set rate and with an initial delay. Here's how I would do it:

// Create member variables for your ExecutorService and ScheduledFuture
private ScheduledExecutorService mExecutor;
private ScheduledFuture&lt;?&gt; mFuture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
final EditText editText = findViewById(R.id.textView);
final Button button = findViewById(R.id.button2);
// Set up the ExecutorService
mExecutor = Executors.newSingleThreadScheduledExecutor();
final Runnable deleteRunnable = new Runnable() {
@Override
public void run() {
// We&#39;ll use the view&#39;s post() method
// to make sure we&#39;re updating it from the correct thread
editText.post(new Runnable() {
@Override
public void run() {
String textValue = editText.getText().toString();
if(textValue.length() &gt; 0){
// Delete the last character
textValue = textValue.substring(0, textValue.length() - 1);
Log.d(&quot;MY_LOG_TAG&quot;, textValue);
editText.setText(textValue);
}
// (Optionally)
// Keep the cursor at the end of the text
editText.setSelection(textValue.length());
}
});
}
};
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
Log.d(&quot;MY_LOG_TAG&quot;, &quot;Action down&quot;);
// Schedule the runnable with an initial delay of 1000 milliseconds
// and at a rate of 250 milliseconds
mFuture = mExecutor.scheduleAtFixedRate(deleteRunnable, 1000, 250, TimeUnit.MILLISECONDS);
break;
case MotionEvent.ACTION_UP:
Log.d(&quot;MY_LOG_TAG&quot;, &quot;Action up&quot;);
// Cancel the scheduled runnable when the Up event is triggered
if(mFuture != null){
mFuture.cancel(false);
}
break;
}
return false;
}
});
}
@Override
protected void onDestroy() {
// Shutdown the Executor Service
mExecutor.shutdown();
super.onDestroy();
}

huangapple
  • 本文由 发表于 2020年10月17日 04:34:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64396068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定