调用Android对话框

时间:2014-04-30 09:17:21

标签: android

我的应用程序中有这样的对话框:

//Dialog de idiomas 
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.language_prompt)
               .setItems(R.array.languages, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                   // The 'which' argument contains the index position
                   // of the selected item
                       switch (which) {
                            case 0:

                             savePreferences("idioma","es");
                             break;
                            case 1:
                              savePreferences("idioma","en");
                              break;

                       }
               }
        });
        return builder.create();

如何通过点击事件调用此对话框?谢谢

2 个答案:

答案 0 :(得分:1)

创建此类片段的实例,并在show()上调用Object

您的参考:http://developer.android.com/intl/es/guide/topics/ui/dialogs.html

答案 1 :(得分:0)

    final Context context = this;
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

        // set title
        alertDialogBuilder.setTitle("");
        // set dialog message
        alertDialogBuilder
        .setMessage("Alert box title")
        .setCancelable(false)
        .setPositiveButton("es",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
         // if this button is clicked, close
        // current activity
 savePreferences("idioma","es");


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

                        savePreferences("idioma","en");
                    }
                });

        // create alert dialog
        alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();