我有一个CardView可以用作启动/停止功能。

启动后,按此键即可使用。但是,停止将不起作用。我知道onClick事件在文本视图更改时起作用,但是实际功能不起作用。任何帮助,不胜感激。我将在下面发布整个代码。

 //Defining Cards on Landing Page
    appsCard = (CardView) findViewById(R.id.apps_card);
    parentalControlsCard = (CardView) findViewById(R.id.parentalControls_id);
    customSettingsCard = (CardView) findViewById(R.id.customSettings);
    activateCard = (CardView) findViewById(R.id.activate_id);
    StartStopCard = (CardView) findViewById(R.id.StartStopCard);


    //Add OnClick Listeners
    appsCard.setOnClickListener(this);
    parentalControlsCard.setOnClickListener(this);
    customSettingsCard.setOnClickListener(this);
    activateCard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (ruleSets.isEmpty()) {
                Toast.makeText(LandingPage.this, "You did not create a custom setting.", Toast.LENGTH_SHORT).show();
            } else {
                PendingIntent pending_start;
                PendingIntent pending_stop;
                Intent startIntent = new Intent(LandingPage.this, LockOptionReceiver.class);
                Intent stopIntent = new Intent(LandingPage.this, LockOptionReceiver.class);
                Calendar startTime = new GregorianCalendar();
                Calendar endTime = new GregorianCalendar();
                String startString = ruleSets.get(0).getStartTime();
                String endString = ruleSets.get(0).getEndTime();

                String[] startArr = startString.split(":");
                String[] endArr = endString.split(":");

                startTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startArr[0]));
                startTime.set(Calendar.MINUTE, Integer.parseInt(startArr[1]));

                endTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endArr[0]));
                endTime.set(Calendar.MINUTE, Integer.parseInt(endArr[1]));

                pending_start = PendingIntent.getBroadcast(LandingPage.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                pending_stop = PendingIntent.getBroadcast(LandingPage.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                startIntent.putExtra("status", "start");
                stopIntent.putExtra("status", "stop");

                Toast.makeText(LandingPage.this, "Your ruleset will start at " + startString, Toast.LENGTH_SHORT).show();

                setStatus("Lock Active");
                setProcess("Stop");

                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY, startTime.getTimeInMillis(), pending_start);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY, startTime.getTimeInMillis(), pending_stop);
            }
        }
    });


    CardView StartStopCard = (CardView) findViewById(R.id.StartStopCard);
    StartStopCard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        mStarted = !mStarted;
            if (mStarted) {
                mStarted=false;
                SharedPreferences setting = getSharedPreferences("PREFS", 0);
                SharedPreferences.Editor editor = setting.edit();
                editor.remove("switcher");
                editor.remove("switcher2");
                editor.remove("lockStatus");
                editor.remove("processStatus");
                editor.commit();
                editor.putString("switcher", "true");
                editor.putString("switcher2", "true");
                editor.putString("lockStatus", "Lock Active");
                editor.putString("processStatus", "Start");
                editor.apply();
                intent.putExtra("status", "start");
                sendBroadcast(intent);
                intent.putExtra("processStatus", "start");
                sendBroadcast(intent);
                String status = setting.getString("lockStatus", "");
                setStatus(status);
                String processStatus = setting.getString("processStatus" , "");
                setProcess(processStatus);

            }
            else {
                mStarted= true;
                SharedPreferences setting = getSharedPreferences("PREFS", 0);
                SharedPreferences.Editor editor = setting.edit();
                editor.remove("switcher");
                editor.remove("switcher2");
                editor.remove("lockStatus");
                editor.remove("processStatus");
                editor.commit();
                editor.putString("switcher", "false");
                editor.putString("switcher2", "false");
                editor.putString("lockStatus", "Lock Deactivated");
                editor.putString("processStatus", "Stop");
                editor.apply();
                intent.putExtra("status", "stop");
                sendBroadcast(intent);
                intent.putExtra("processStatus", "start");
                sendBroadcast(intent);
                String status = setting.getString("lockStatus", "");
                setStatus(status);
                String processStatus = setting.getString("processStatus" , "");
                setProcess(processStatus);

            }
        }

    });

}


................................................... ......................................

最佳答案

问题在这里

 mStarted = !mStarted;
        if (mStarted) {
            mStarted=false;
            ...
        else{
            mStarted = true;
        ...
        }


假设您的mStartedtrue开头。在if语句之前将其还原,然后再次转到true,因为它转到else。当您按下按钮时,它将执行相同的操作,它将被还原并转到其他位置。如果mStarted从一开始就为假,则相同。关键是,它将始终转到同一分支。

如果要切换点击行为(每次点击进入if语句的不同分支),则可以从分支中删除mStarted = !mStarted;或删除mStarted分配。

08-17 22:30