0

我正在尝试查看带有单选按钮的对话框,但是当我单击按钮时,警报对话框永远不会出现。下面是代码。请让我知道是否有任何替代解决方案。此示例取自 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

4 回答 4

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();
}
于 2013-05-16T19:36:09.083 回答
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();
        }   
  }
}
于 2013-05-16T19:30:29.037 回答
0

你在里面做错了onCreateDialog()

首先,您不应该id = DIALOG_SINGLE_COICE只测试idvalue 是否等于DIALOG_SINGLE_CHOICE. * 如果是,则创建DialogwithAlertDialog.Builder和 return builder.create()。* 如果没有,返回super.onCreateDialog(id).

但是这种方法已被弃用,您应该DialogFragment改用。

于 2013-05-16T19:34:49.253 回答
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);
}
}
于 2013-05-17T09:27:50.027 回答