英文:
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<?> 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'll use the view's post() method
// to make sure we're updating it from the correct thread
editText.post(new Runnable() {
@Override
public void run() {
String textValue = editText.getText().toString();
if(textValue.length() > 0){
// Delete the last character
textValue = textValue.substring(0, textValue.length() - 1);
Log.d("MY_LOG_TAG", 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("MY_LOG_TAG", "Action down");
// 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("MY_LOG_TAG", "Action up");
// 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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论