質問

How do I change the state of toggleButton Only after I click OK on a dialog box , and if i clicked Cancel toggleButton is not changed , my problem is toggleButton is switched always,So Is there an example or a specific implementation that needs to be done before ?

 toggle1 = (ToggleButton) findViewById(R.id.filterButton);
 //   toggle1.setChecked(getDefaults("Toggle1S",this));
 //  setDefaults("Toggle1S", toggle1.isChecked(), this);

toggle1.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        if (toggle1.isChecked()) {
            on.start();
            Using();
            if(Use.equals("1"))
            {//Wifi Function
                Toast.makeText(getApplicationContext(), "Filter ON sent using Wifi", Toast.LENGTH_SHORT).show();
                new MyAsyncTask().execute("filter_st","ON");
            }
            else{
                String temp = "Are you want to turn Filter ON using GSM";
                callCheck(SMStitle,temp);
                sm.sendTextMessage(number, null, messages[0], null, null);
                Toast.makeText(getApplicationContext(), "Filter ON sent using GSM number"+number, Toast.LENGTH_SHORT).show();                               

            }

    public void callCheck(String c,String d)
    {
        // Creating alert Dialog with one Button

        AlertDialog alertDialog1 = new AlertDialog.Builder(
        fishtank.this).create();

        // Setting Dialog Title
        alertDialog1.setTitle(c);

        // Setting Dialog Message
        alertDialog1.setMessage(d);


        // Setting OK Button
        alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                //Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog1.setButton("Cancel", new DialogInterface.OnClickListener()    {
               public void onClick(DialogInterface dialog, int which) {
               }
        });

        // Showing Alert Message
        alertDialog1.show();
    }
役に立ちましたか?

解決

as i understand your question seem to be fix easily consider this code
1-

alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                       toggelBt.toggle();
                }
            });

2- and on other way u can save your State On runtime in boolean Variable in each calling ToggleButton Onclick method (using sharedPreference instead boolean for saving and using on next app startup) then

 Boolean CheckState
 ...
 alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                       if(checkState)//should set uncheck now
                             toggelBt.setChecked(false);
                       else
                             toggelBt.setChecked(true);
                }
            });

hop to be usefull :)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top