自定义对话框onBackPressed

时间:2014-08-13 07:16:23

标签: android xml eclipse dialog

我的button Yesbutton No功能在我的自定义对话 后退中有问题。

这是我的代码:

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();

    }

}

我的项目似乎正在运行,但每次按下按钮是,按钮会在程序关闭的对话框上显示。我认为dismiss function不起作用?为什么?请帮忙。请告诉像我一样的菜鸟。谢谢你们,还有那些不值钱的人。

更新:继承我的XML

<?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>

1 个答案:

答案 0 :(得分:0)

                //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();  


                    }});