简体   繁体   English

单击按钮时显示AlertDialog

[英]Show an AlertDialog when button is clicked

I am trying to view a dialog box with radio buttons but when I clicked on the button alert dialog box never appears. 我正在尝试查看带有单选按钮的对话框,但是当我单击按钮警报对话框时,它从未出现。 Below is the code. 下面是代码。 Please let me know if there is any alternate solution to this. 请让我知道是否还有其他解决方法。 This sample was taken from API Demos. 此示例摘自API演示。

public class MainPage extends Activity{
Button start;
private static final int DIALOG_SINGLE_CHOICE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainpage);
    start = (Button) findViewById(R.id.start);
    start.setBackgroundResource(R.drawable.read);
    start.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {

            showDialog(DIALOG_SINGLE_CHOICE );

        }
    });

}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
    id = DIALOG_SINGLE_CHOICE;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose your option");
    builder.setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    })
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Yes so do some stuff */
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked No so do some stuff */
                }
            })
           .create();
    return super.onCreateDialog(id);
        }   
  }
}

Create this function and use showAskDialog() function in your click event 创建此函数并在click事件中使用showAskDialog()函数

private void showAskDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("DialogTitle").setMessage("DialogMessage").setCancelable(true).setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  // TODO Auto-generated method stub

                }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            }).setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            });
    // before @TronicZomB advice
    //AlertDialog alert = builder.create();
    //alert.show();
    // after @TronicZomB advice
    builder.show();
}

You need to replace return super.onCreateDialog(id); 您需要替换return super.onCreateDialog(id); with return builder.show(); return builder.show(); . .create() will create an alert dialog from the builder but it does not display that dialog. .create()将通过构建器创建一个警报对话框,但不会显示该对话框。 .show() will create the dialog from the builder and show it on the screen. .show()将通过构建器创建对话框并将其显示在屏幕上。

I also think that the int id might be unnecessary. 我也认为int id可能是不必要的。 You could try the following code and it should work for you: 您可以尝试以下代码,它应该可以为您工作:

public class MainPage extends Activity{
Button start;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
start = (Button) findViewById(R.id.start);
start.setBackgroundResource(R.drawable.read);
start.setOnClickListener(new View.OnClickListener() {
    @SuppressWarnings("deprecation")
    public void onClick(View v) {

        showDialog(DIALOG_SINGLE_CHOICE );

    }
});

}

@Override
public Dialog onCreateDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose your option");
    builder.setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    })
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Yes so do some stuff */
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked No so do some stuff */
                }
            });
        return builder.show();
        }   
  }
}

You're doing it wrong inside onCreateDialog() . 您在onCreateDialog()做错了。

First, you shouldn't do id = DIALOG_SINGLE_COICE but test id value if it equals to DIALOG_SINGLE_CHOICE . 首先,您不应该执行id = DIALOG_SINGLE_COICE而是测试id值是否等于DIALOG_SINGLE_CHOICE * If yes, then create the Dialog with AlertDialog.Builder and return builder.create() . *如果是,则使用AlertDialog.Builder创建Dialog并返回builder.create() * If no, return super.onCreateDialog(id) . *如果否,则返回super.onCreateDialog(id)

But this method is deprecated, you should use DialogFragment instead. 但是不建议使用此方法,而应使用DialogFragment

public class MainActivity extends Activity implements OnClickListener{
private Button button;
private static final int DIALOG_SINGLE_CHOICE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button  = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button:
        showDialog(DIALOG_SINGLE_CHOICE);
        break;

    default:
        break;
    }
}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose your option");
    builder.setSingleChoiceItems(R.array.Baani, 0, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        }

    }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            /* User clicked Yes so do some stuff */
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            /* User clicked No so do some stuff */
        }
    }).create().show();
    return super.onCreateDialog(id);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM