【问题标题】:Custom Dialog on BackPressed自定义对话框 onBackPressed
【发布时间】:2014-08-13 07:16:22
【问题描述】:

我的button Yesbutton NoBackpressed 中的自定义对话框 上的功能存在问题。

这是我的代码:

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>

【问题讨论】:

    标签: android xml eclipse dialog


    【解决方案1】:
                    //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();  
    
    
                        }});
    

    【讨论】:

    • 这是我的 Backpressed 之前的样子。但现在我试图通过 xml 创建一个 customize alertdialog。对不起,@krunal 先生,但我认为这不是答案。
    • 没有错误。只是btnNo和btnYes的功能不起作用。
    • 查看代码,如果是“是”,我将关闭活动,如果是“否”,我将关闭对话框。现在由你决定,现在你想使用它
    猜你喜欢
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 2012-08-13
    • 2013-01-06
    • 2012-08-27
    • 2011-06-04
    • 1970-01-01
    相关资源
    最近更新 更多