zoukankan      html  css  js  c++  java
  • 自动点击按钮事件+解决Toast重复出现问题

     private Button btn;
        int i = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn = (Button)findViewById(R.id.btn);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    i++;
                    showToast("ok"+i);
                }
            });
            btn.dispatchTouchEvent(
                    MotionEvent.obtain(SystemClock.uptimeMillis(),
                            SystemClock.uptimeMillis(),
                            MotionEvent.ACTION_DOWN,
                            0,
                            0,
                            0));
            btn.dispatchTouchEvent(
                    MotionEvent.obtain(SystemClock.uptimeMillis(),
                            SystemClock.uptimeMillis(),
                            MotionEvent.ACTION_UP,
                            0,
                            0,
                            0));
        }
        private Toast mToast;
        public void showToast(String text) {
            if(mToast == null) {
                mToast = Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT);
            } else {
                mToast.setText(text);
                mToast.setDuration(Toast.LENGTH_SHORT);
            }
            mToast.show();
        }
    
        public void cancelToast() {
            if (mToast != null) {
                mToast.cancel();
            }
        }
    
        public void onBackPressed() {
            cancelToast();
            super.onBackPressed();
        }
  • 相关阅读:
    Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
    Django的ORM操作
    RabbitMQ
    CentOS忘记用户名或者密码解决办法
    VUE-es6
    vue基础之命令
    爬虫框架:scrapy
    爬虫高性能相关
    MongoDB
    Beautifulsoup模块
  • 原文地址:https://www.cnblogs.com/tero/p/5461729.html
Copyright © 2011-2022 走看看