Android活动按钮onClick()侦听器运行缓慢

时间:2018-11-04 10:51:08

标签: android android-activity

在我的“活动”按钮中,事件运行缓慢,在此活动中,所有按钮均正常运行,但它们在2/3秒左右的响应时间很少。我在这里单击按钮时会更改所有按钮背景。我不知道出了什么问题,请帮帮我!

  

活动文件

"bootstrap": "^4.1.3",
"jquery": "^3.3.1",
"popper.js": "^1.14.4"

}

  

xml文件

public class TaskBarView extends Activity implements OnClickListener {

private int time = 0;
private Button btn_fiveSec,btn_tenSec,btn_fifteenSec,btn_thirtySec,btn_oneMin,btn_fiveMin,btn_tenMin,btn_thirtyMin;
private TextView tv_viewTime;
Handler myHandler;
boolean startPrank;

public void onBackPressed() {
    finish();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myHandler = new Handler();

    final Runnable mMyRunnable = new Runnable() {
        @Override
        public void run(){

            if(Build.VERSION.SDK_INT >= 23) {
                if (!Settings.canDrawOverlays(TaskBarView.this)) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                    startActivityForResult(intent, 1234);
                } else {
                    playSound();
                    Intent intent = new Intent(TaskBarView.this, CrackScreen.class);
                    startService(intent);
                    finish();
                }
            } else{
                playSound();
                Intent intent = new Intent(TaskBarView.this, CrackScreen.class);
                startService(intent);
                finish();
            }
        }
    };

    Button btn_startPrank = findViewById(R.id.btn_startPrank);
    btn_startPrank.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            startPrank = true;

            showToast(startPrank);

            int de = time * 1000;
            myHandler.postDelayed(mMyRunnable, de);
        }
    });

    btn_fiveSec = findViewById(R.id.btn_fiveSec);
    btn_fiveSec.setOnClickListener(this);

    btn_tenSec = findViewById(R.id.btn_tenSec);
    btn_tenSec.setOnClickListener(this);

    btn_fifteenSec = findViewById(R.id.btn_fifteenSec);
    btn_fifteenSec.setOnClickListener(this);

    btn_thirtySec = findViewById(R.id.btn_thirtySec);
    btn_thirtySec.setOnClickListener(this);

    btn_oneMin = findViewById(R.id.btn_oneMin);
    btn_oneMin.setOnClickListener(this);

    btn_fiveMin = findViewById(R.id.btn_fiveMin);
    btn_fiveMin.setOnClickListener(this);

    btn_tenMin = findViewById(R.id.btn_tenMin);
    btn_tenMin.setOnClickListener(this);

    btn_thirtyMin = findViewById(R.id.btn_thirtyMin);
    btn_thirtyMin.setOnClickListener(this);


    Button btn_stopPrank = findViewById(R.id.btn_stopPrank);
    btn_stopPrank.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            myHandler.removeCallbacks(mMyRunnable);
            startPrank = false;
            Intent intent = new Intent(TaskBarView.this, CrackScreen.class);
            stopService(intent);
            tv_viewTime.setText("Cracked Screen stopped");

            showToast(startPrank);
        }
    });

    tv_viewTime = findViewById(R.id.tv_viewTime);
    tv_viewTime.setText("Cracked Screen starts with in 0 second");

}

private void playSound(){
    MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.glass_break);
    mp.start();
}

private void showToast(boolean prank){
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

    TextView tv_toastMessage = layout.findViewById(R.id.tv_toastMessage);
    RelativeLayout relativeLayout = layout.findViewById(R.id.re_toastBackground);

    if(startPrank){
        relativeLayout.setBackgroundResource(R.drawable.notification_start);
        tv_toastMessage.setText("Start Pranking");
        tv_toastMessage.setTextColor(Color.parseColor("#0AB1E9"));
    } else if(!startPrank){
        relativeLayout.setBackgroundResource(R.drawable.notification_stop);
        tv_toastMessage.setText("Stop Pranking");
        tv_toastMessage.setTextColor(Color.parseColor("#F83668"));
    }else {
        relativeLayout.setBackgroundColor(Color.parseColor("#6651D4"));
        tv_toastMessage.setText("Just Fun");
    }

    Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER,0,0);
    toast.setView(layout);
    toast.show();
}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn_fiveSec:
            time = 5;

            btn_fiveSec.setBackgroundResource(R.drawable.time_select);
            btn_fiveSec.setTextColor(Color.WHITE);
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 5 second");

            break;

        case R.id.btn_tenSec:
            time = 10;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time_select);
            btn_tenSec.setTextColor(Color.WHITE);
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 10 second");

            break;

        case R.id.btn_fifteenSec:
            time = 15;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time_select);
            btn_fifteenSec.setTextColor(Color.WHITE);
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 15 second");

            break;

        case R.id.btn_thirtySec:
            time = 30;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time_select);
            btn_thirtySec.setTextColor(Color.WHITE);
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 30 second");

            break;

        case R.id.btn_oneMin:
            time = 60;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time_select);
            btn_oneMin.setTextColor(Color.WHITE);
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 1 Minute");

            break;

        case R.id.btn_fiveMin:
            time = 300;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time_select);
            btn_fiveMin.setTextColor(Color.WHITE);
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 5 Minute");

            break;

        case R.id.btn_tenMin:
            time = 600;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time_select);
            btn_tenMin.setTextColor(Color.WHITE);
            btn_thirtyMin.setBackgroundResource(R.drawable.time);
            btn_thirtyMin.setTextColor(Color.parseColor("#6651D4"));
            tv_viewTime.setText("Cracked Screen starts with in 10 Minute");

            break;

        case R.id.btn_thirtyMin:
            time = 1800;

            btn_fiveSec.setBackgroundResource(R.drawable.time);
            btn_fiveSec.setTextColor(Color.parseColor("#6651D4"));
            btn_tenSec.setBackgroundResource(R.drawable.time);
            btn_tenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_fifteenSec.setBackgroundResource(R.drawable.time);
            btn_fifteenSec.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtySec.setBackgroundResource(R.drawable.time);
            btn_thirtySec.setTextColor(Color.parseColor("#6651D4"));
            btn_oneMin.setBackgroundResource(R.drawable.time);
            btn_oneMin.setTextColor(Color.parseColor("#6651D4"));
            btn_fiveMin.setBackgroundResource(R.drawable.time);
            btn_fiveMin.setTextColor(Color.parseColor("#6651D4"));
            btn_tenMin.setBackgroundResource(R.drawable.time);
            btn_tenMin.setTextColor(Color.parseColor("#6651D4"));
            btn_thirtyMin.setBackgroundResource(R.drawable.time_select);
            btn_thirtyMin.setTextColor(Color.WHITE);
            tv_viewTime.setText("Cracked Screen starts with in 30 Minute");

            break;

    }
}

我在这里使用线程。任何建议都可以视为帮助。谢谢!

0 个答案:

没有答案