java切换Android应用程序中基于开关的对话框按钮选择
我正在尝试构建我的第一个Android应用程序,但我已经被卡住了。我有两个切换开关,当它们打开时,会出现一个对话框窗口。我想用“取消”按钮把开关关掉。我试过切换按钮。setChecked(错误),开关。setChecked(false)等,但由于这些开关是在XML文件中创建的,因此没有对象可用于执行该方法。如何在程序中切换这些开关?在我的主要活动中有我的onClick监听器,对话创建是另一个类。这可能是错误的,但在这一点上是有效的
主要活动。爪哇:
package com.example.arduinoautomation;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.View;
import 安卓.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;
}
}
兰彭迪亚洛。爪哇:
package com.example.arduinoautomation;
import 安卓.app.AlertDialog;
import 安卓.app.Dialog;
import 安卓.app.DialogFragment;
import 安卓.content.DialogInterface;
import 安卓.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();
}
}
主要活动。xml
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Switch
安卓:id="@+id/switch1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentLeft="true"
安卓:layout_alignParentTop="true"
安卓:layout_marginTop="57dp"
安卓:onClick="lampToggle"
安卓:text="Lamp" />
<Switch
安卓:id="@+id/switch2"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentLeft="true"
安卓:layout_below="@+id/switch1"
安卓:layout_marginTop="49dp"
安卓:onClick="lightToggle"
安卓:text="Lights" />
</RelativeLayout>
# 1 楼答案
您必须将ToggleButton定义为活动中的视图:
然后实例化它,通常在onCreate方法中:
然后,您可以在视图中的任何位置使用setChecked方法:
编辑
您无法访问该视图,因为您的对话框是另一个类,而切换视图位于活动类中。尝试在活动类内创建对话框:
然后通过调用
showDialog()
方法从活动中的任何位置显示一个对话框: