Android应用中的确认框中未显示“是”按钮?

时间:2014-02-10 13:23:06

标签: android android-layout

朋友们,我设置了一个带有是和否按钮的对话框,但只有NO是可见的YES按钮不可见请建议我

这是我提前感谢的代码

     alertDialog.setButton("YES", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,int which) {

                 // Write your code here to invoke YES event
                 Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
                 }
             });

             // Setting Negative "NO" Button
    alertDialog.setButton("NO bad", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
                 // Write your code here to invoke NO event
                 Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                 dialog.cancel();
                 }
             }); 

3 个答案:

答案 0 :(得分:3)

我认为你应该遵循下面给出的这个Builder模式示例。

AlertDialog.Builder builder = new AlertDialog.Builder(context)
        .setCancelable(true)
        .setTitle(titleResourceId)
        .setMessage(messageResourceId)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeToast(mContext, "Yes", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        }).setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeToast(mContext, "No", Toast.LENGTH_SHORT).show();
            }
        });
    builder.show();

使用setPositiveButton()setNegativeButton()允许Android根据应用运行的平台按正确的顺序放置按钮。

答案 1 :(得分:2)

而不是

alertDialog.setButton 

使用

alertDialog.setPositiveButton
alertDialog.setNegativeButton

答案 2 :(得分:1)

而不是

setButton()

setPositiveButton()用于“是”,将setNegativeButton()用于“否”