简体   繁体   中英

ToggleButton onCheckChangeListener

I'm making an app that has a record button and a togglebutton. The record button starts recording sound and the togglebutton declares whether the record button has to be HELD to record or only pressed once. This is how I'm trying to achieve it:

touchToRecord.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            saveCheckedState("touchToRecord", isChecked);

            if (isChecked)
            {
                recBtn.setOnTouchListener(new OnTouchListener(){

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {

                        if (event.getAction() == MotionEvent.ACTION_DOWN)
                        {
                            recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
                            chTimer.setBase(SystemClock.elapsedRealtime());
                            chTimer.setText("00:00:00");
                            chTimer.start();
                            chTimer.setTextColor(Color.GREEN);
                            startRecording();
                        }
                        else if (event.getAction() == MotionEvent.ACTION_UP)
                        {
                            recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
                            chTimer.stop();
                            chTimer.setBase(SystemClock.elapsedRealtime());
                            chTimer.setText("00:00:00");
                            chTimer.setTextColor(Color.GRAY);
                            stopRecording();
                            nameAlert();
                        }
                        return true;
                    }
                });
            }
            else if (isChecked == false)
            {

            }

As you can see, if togglebutton is 'on' or 'checked', the record button has to be held down to record, and when you let og of the button, it stops recording. Now this works but it only have one flaw. I am saving the isChecked state to sharedpreferences, so if I turn the togglebutton 'on' and then restart the app, the togglebutton will remain 'on', but I'll have to turn it back off and on again for it to work. So if I restart the app, it'll be still 'on' but it won't work.

Also, I haven't yet written the code for what happens if the togglebutton is false, as you can see.

My code now:

if (touchToRecord.isChecked() == true)
    {
         recBtn.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
                    startTimer();
                    startRecording();
                }
                else if (event.getAction() == MotionEvent.ACTION_UP)
                {
                    recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
                    stopTimer();
                    stopRecording();
                    nameAlert();
                }
            return true;
            }
        });
    }

    if (touchToRecord.isChecked() == false)
    {
        recBtn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                if (recorder == null)
                {
                    recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
                    startTimer();
                    startRecording();
                }
                else if (recorder != null)
                {
                    recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
                    stopTimer();
                    stopRecording();
                    nameAlert();
                }
            }
        });
    }

OnCheckedChangeListener:

touchToRecord.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            saveCheckedState("touchToRecord", isChecked);
        }
    });

Get togglebutton state from shared preferences:

touchToRecord.setChecked(getCheckedState("touchToRecord"));

It is about your logic. You have onCheckedChangeListener , which means you will check if it is On or Off when user change the Toggle Button . So when you kill app and start again, since there is no change on Toggle Button , it won't work.

Put your onTouchListener outside the onCheckedChangeListener . In your onCheckedChangeListener set your status( On or Off ) and inside the onTouchListener check if it is On or Off and perform your actions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM