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

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

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

问题

清除按钮:

  1. clear.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. if (operation.length() != 0) {
  5. String s = operation.getText().toString();
  6. s = s.substring(0, operation.length() - 1);
  7. operation.setText(s);
  8. }
  9. }
  10. });

长按触发想法:

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

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:

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

Long touch idea:

  1. clear.setOnTouchListener(new View.OnTouchListener() {
  2. @Override
  3. public boolean onTouch(View v, MotionEvent event) {
  4. return false;
  5. }
  6. });

答案1

得分: 1

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

  1. // 为ExecutorService和ScheduledFuture创建成员变量
  2. private ScheduledExecutorService mExecutor;
  3. private ScheduledFuture<?> mFuture;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_test);
  8. final EditText editText = findViewById(R.id.textView);
  9. final Button button = findViewById(R.id.button2);
  10. // 设置ExecutorService
  11. mExecutor = Executors.newSingleThreadScheduledExecutor();
  12. final Runnable deleteRunnable = new Runnable() {
  13. @Override
  14. public void run() {
  15. // 我们将使用视图的post()方法
  16. // 以确保我们从正确的线程更新它
  17. editText.post(new Runnable() {
  18. @Override
  19. public void run() {
  20. String textValue = editText.getText().toString();
  21. if (textValue.length() > 0) {
  22. // 删除最后一个字符
  23. textValue = textValue.substring(0, textValue.length() - 1);
  24. Log.d("MY_LOG_TAG", textValue);
  25. editText.setText(textValue);
  26. }
  27. // (可选)
  28. // 保持光标在文本末尾
  29. editText.setSelection(textValue.length());
  30. }
  31. });
  32. }
  33. };
  34. button.setOnTouchListener(new View.OnTouchListener() {
  35. @Override
  36. public boolean onTouch(View v, MotionEvent event) {
  37. int action = event.getAction();
  38. switch (action) {
  39. case MotionEvent.ACTION_DOWN:
  40. Log.d("MY_LOG_TAG", "按下动作");
  41. // 使用初始延迟1000毫秒和速率250毫秒调度可运行项
  42. mFuture = mExecutor.scheduleAtFixedRate(deleteRunnable, 1000, 250, TimeUnit.MILLISECONDS);
  43. break;
  44. case MotionEvent.ACTION_UP:
  45. Log.d("MY_LOG_TAG", "抬起动作");
  46. // 当触发Up事件时取消已调度的可运行项
  47. if (mFuture != null) {
  48. mFuture.cancel(false);
  49. }
  50. break;
  51. }
  52. return false;
  53. }
  54. });
  55. }
  56. @Override
  57. protected void onDestroy() {
  58. // 关闭Executor Service
  59. mExecutor.shutdown();
  60. super.onDestroy();
  61. }
英文:

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

  1. // Create member variables for your ExecutorService and ScheduledFuture
  2. private ScheduledExecutorService mExecutor;
  3. private ScheduledFuture&lt;?&gt; mFuture;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_test);
  8. final EditText editText = findViewById(R.id.textView);
  9. final Button button = findViewById(R.id.button2);
  10. // Set up the ExecutorService
  11. mExecutor = Executors.newSingleThreadScheduledExecutor();
  12. final Runnable deleteRunnable = new Runnable() {
  13. @Override
  14. public void run() {
  15. // We&#39;ll use the view&#39;s post() method
  16. // to make sure we&#39;re updating it from the correct thread
  17. editText.post(new Runnable() {
  18. @Override
  19. public void run() {
  20. String textValue = editText.getText().toString();
  21. if(textValue.length() &gt; 0){
  22. // Delete the last character
  23. textValue = textValue.substring(0, textValue.length() - 1);
  24. Log.d(&quot;MY_LOG_TAG&quot;, textValue);
  25. editText.setText(textValue);
  26. }
  27. // (Optionally)
  28. // Keep the cursor at the end of the text
  29. editText.setSelection(textValue.length());
  30. }
  31. });
  32. }
  33. };
  34. button.setOnTouchListener(new View.OnTouchListener() {
  35. @Override
  36. public boolean onTouch(View v, MotionEvent event) {
  37. int action = event.getAction();
  38. switch(action){
  39. case MotionEvent.ACTION_DOWN:
  40. Log.d(&quot;MY_LOG_TAG&quot;, &quot;Action down&quot;);
  41. // Schedule the runnable with an initial delay of 1000 milliseconds
  42. // and at a rate of 250 milliseconds
  43. mFuture = mExecutor.scheduleAtFixedRate(deleteRunnable, 1000, 250, TimeUnit.MILLISECONDS);
  44. break;
  45. case MotionEvent.ACTION_UP:
  46. Log.d(&quot;MY_LOG_TAG&quot;, &quot;Action up&quot;);
  47. // Cancel the scheduled runnable when the Up event is triggered
  48. if(mFuture != null){
  49. mFuture.cancel(false);
  50. }
  51. break;
  52. }
  53. return false;
  54. }
  55. });
  56. }
  57. @Override
  58. protected void onDestroy() {
  59. // Shutdown the Executor Service
  60. mExecutor.shutdown();
  61. super.onDestroy();
  62. }

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:

确定