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

标签 java android switch-statement android-alertdialog

我正在尝试构建我的第一个 Android 应用程序,但我已经卡住了。我有两个拨动开关,当它们打开时,会出现一个对话窗口。我想要“取消”按钮来关闭开关。我试过 toggleButton.setChecked(false)、Switch.setChecked(false) 等,但是因为这些开关是在 XML 文件中创建的,所以没有对象可以执行该方法。如何在我的程序中切换这些开关?我在我的主要 Activity 中有我的 onClick 监听器,并将对话框创建为不同的类。这可能是错误的,但到目前为止它是有效的。

主要 Activity .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>

最佳答案

您必须在 Activity 中将 ToggleButton 定义为 View :

ToggleButton toggle;

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

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

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

toggle.setChecked(false);

编辑

你不能访问View,因为你的dialog是另外一个类,而toggle 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() 方法在您的 Activity 中的任何位置显示一个对话框:

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

关于java - 在 Android 应用程序中切换基于关闭对话框按钮选择的开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21318781/

相关文章:

java - Java接口(interface)的switch语句

java - TextView 未出现在 onDraw 对象中

java - 在 Mac 上的 Java 应用程序中禁用弹出菜单

Android 开发 - 回调 URL 无效... (0_o)

java - 使用十六进制值设置 Android 图像

javascript - 如何在 switch 语句中捆绑 "data-"属性的案例?

java - 如何为 Multi-Tenancy 配置 Spring Oauth2

java - JNI无法编译动态库

java - 检查通话中断 - Android Lollipop

python - 如何在python case语句中运行代码块