在Android应用程序中切换基于关闭对话框按钮选项的开关

时间:2014-01-23 20:36:26

标签: java android switch-statement alertdialog

我正在尝试构建我的第一个Android应用程序,而且我已经被卡住了。我有两个切换开关,当它们打开时,会出现一个对话窗口。我想要“取消”按钮关闭开关。我已经尝试过toggleButton.setChecked(false),Switch.setChecked(false)等,但由于这些开关是在XML文件中创建的,因此无法在/上执行该方法。如何在程序中切换这些开关?我在我的主要活动中有我的onClick监听器,并且对话框创建为另一个类。这可能是错的,但它可以解决这个问题。

MainActivity.java:

package com.example.arduinoautomation;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Switch;

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void lampToggle(View view) {
        // Is the toggle on?
        boolean on = ((Switch) view).isChecked();

        if (on) {
            lampOnDialog lamp_on = new lampOnDialog();
            lamp_on.message = "Lamp is on.";
            lamp_on.show(this.getFragmentManager(),"switch");
        } else {
            // Disable vibrate
        }
    }

    public void lightToggle(View view) {
        // Is the toggle on?
        boolean on = ((Switch) view).isChecked();

        if (on) {
            lampOnDialog light_on = new lampOnDialog();
            light_on.message = "Light is on";
            light_on.show(this.getFragmentManager(), "switch");
        } else {
            // Disable vibrate
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

lampOnDialog.java:

package com.example.arduinoautomation;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

public class lampOnDialog extends DialogFragment {
    String message = "";
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(message)
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog

                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="57dp"
        android:onClick="lampToggle"
        android:text="Lamp" />

    <Switch
        android:id="@+id/switch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/switch1"
        android:layout_marginTop="49dp"
        android:onClick="lightToggle"
        android:text="Lights" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

您必须在活动中将ToggleButton定义为视图:

ToggleButton toggle;

然后实例化它,通常在你的onCreate方法中:

toggle = (ToggleButton) findViewById(R.id.switch1);

然后,您可以在任何地方使用setChecked方法:

toggle.setChecked(false);

修改

您无法访问View,因为您的对话框是另一个类,并且切换视图位于您的Activity类中。尝试在Activity类中创建对话框:

public void showDialog(String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(message).setPositiveButton("Yes, you will", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            toggle.setChecked(true);
        }
    }).setNegativeButton("No, you won't", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            toggle.setChecked(false);
        }
    }).show();
}

然后通过调用showDialog()方法显示活动中任意位置的对话框:

showDialog("Hi, I'll be your dialog today");