单击按钮时显示AlertDialog

时间:2013-05-16 19:27:52

标签: android radio-button dialog

我正在尝试查看带有单选按钮的对话框,但是当我单击按钮警告对话框时,永远不会出现。下面是代码。如果有任何替代解决方案,请告诉我。此示例取自API Demos。

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

4 个答案:

答案 0 :(得分:2)

创建此功能并在点击事件中使用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();
}

答案 1 :(得分:1)

您需要将return super.onCreateDialog(id);替换为return builder.show();.create()将从构建器创建警告对话框,但不显示该对话框。 .show()将从构建器创建对话框并在屏幕上显示。

我也认为int id可能是不必要的。您可以尝试以下代码,它应该适合您:

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

答案 2 :(得分:0)

你在onCreateDialog()内做错了。

首先,如果id = DIALOG_SINGLE_COICE等于id,则不应该DIALOG_SINGLE_CHOICE,而是测试Dialog值。 *如果是,请使用AlertDialog.Builder创建builder.create()并返回super.onCreateDialog(id)。 *如果不是,请返回{{1}}。

但是不推荐使用此方法,您应该使用DialogFragment代替。

答案 3 :(得分:0)

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