【问题标题】:Toggle a switch based off dialog button choice in Android application在 Android 应用程序中切换基于关闭对话框按钮选择的开关
【发布时间】:2014-01-23 20:36:25
【问题描述】:

我正在尝试构建我的第一个 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>

【问题讨论】:

    标签: java android switch-statement android-alertdialog


    【解决方案1】:

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

    ToggleButton toggle;
    

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

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

    然后,您可以从任何地方对您的视图使用 setChecked 方法:

    toggle.setChecked(false);
    

    编辑

    您无法访问视图,因为您的对话框是另一个类,而切换视图位于您的 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");
    

    【讨论】:

    • 我能够做到前两行没问题,但是当我尝试在 AlertDialog 的“取消”按钮的 onClick 方法中将其设置为 false 时,它​​告诉我没有变量“toggle 。”
    • 请看我更新的答案。我不确定这是否是最佳解决方案,但它应该可以工作。答案中的所有代码都应该在同一个 Activity 中,因此您可以丢弃 LampOnDialog 类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2023-03-30
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多