Android 反压时的自定义对话框
我的自定义对话框中的按钮“是”和“否”的功能存在问题 这是我的密码:Android 反压时的自定义对话框,android,xml,eclipse,dialog,Android,Xml,Eclipse,Dialog,我的自定义对话框中的按钮“是”和“否”的功能存在问题 这是我的密码: public class menu extends Activity implements OnClickListener { static int nextmaze; Dialog dialog; Button btnNo, btnYes; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated me
public class menu extends Activity implements OnClickListener {
static int nextmaze;
Dialog dialog;
Button btnNo, btnYes;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
Button btnplay = (Button) findViewById(R.id.btnPlay);
Button btnHigh = (Button) findViewById(R.id.btnHighScores);
Button btnSettings = (Button) findViewById(R.id.btnSettings);
btnplay.setOnClickListener(this);
btnHigh.setOnClickListener(this);
btnSettings.setOnClickListener(this);
}
protected void showCustomDialog() {
dialog = new Dialog(this, android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.backpressdialog);
Button btnNo = (Button) dialog.findViewById(R.id.btnNo);
btnNo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
Button btnYes = (Button) dialog.findViewById(R.id.btnYes);
btnYes.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
finish();
}
});
dialog.show();
}
@Override
public void onBackPressed() {
showCustomDialog();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSettings:
Intent prefs = new Intent(menu.this, Setting.class);
startActivity(prefs);
break;
case R.id.btnHighScores:
Intent surface = new Intent(menu.this, SurfaceViewExample.class);
startActivity(surface);
break;
case R.id.btnPlay:
startnextmaze();
}
}
我的项目似乎正在运行,但每次我按下对话框上的“是”和“否”按钮时,程序都会关闭。我认为解雇功能不起作用?为什么?
更新:这是我的XML
这就是我的背压以前的样子。但现在我正试图通过xml创建一个自定义alertdialog。对不起,先生@krunal,但我认为这不是答案。没有错误。只是btnNo和btnYes的功能不起作用。如果是,请查看代码。如果是,我将关闭活动,如果不是,我将关闭对话框。现在由你决定现在你想用它
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/bgdialog"
android:orientation="vertical" >
<Button
android:id="@+id/btnNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialogno" />
<Button
android:id="@+id/btnYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialogyes" />
</RelativeLayout>
//Declaration
RadioButton gallery,camera;
RadioGroup select;
//xml dialog_select_gallery
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/select"
android:layout_width="match_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_height="wrap_content"
android:alpha=".75" >
<RadioButton
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="GALLERY"
android:textSize="20sp" />
<RadioButton
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_marginTop="3dp"
android:text="CAMERA"
android:textSize="20sp" />
</RadioGroup>
</LinearLayout>
// Now Your Dialog
final Dialog dialog = new Dialog(YourActivityNAme.this);
//dialog.getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.dialog_select_gallery);
//dialog.getWindow().setBackgroundDrawable(
//new ColorDrawable(Color.argb(255, 204, 255, 229)));
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.setTitle("SELECT");
dialog.show();
select=(RadioGroup)dialog.findViewById(R.id.select);
gallery =(RadioButton)dialog.findViewById(R.id.gallery);
gallery.setChecked(false);
camera =(RadioButton)dialog.findViewById(R.id.camera);
camera.setChecked(false);
camera.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
dialog.dismiss();
}});
gallery.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
dialog.dismiss();
}});