问题描述
- Android应用开发 对话框创建
-
private void showDialog(String msg){
Builder builder =**new AlertDialog.Builder(HttpUtil.this);**
// AlertDialog.Builder builder =new Builder(HttpUtil.this);
builder.setMessage(msg)
.setCancelable(false)
.setPositiveButton("确定", new DialogInterface.OnClickListener(){
public void onClick (DialogInterface dialog,int id){} }); AlertDialog alert =builder.create(); alert.show();
}
加粗部分提示The constructor AlertDialog.Builder(HttpUtil) is undefined
解决方案
new AlertDialog.Builder需要传递的参数是上下文,你的HttpUtil.this是什么?如果HttpUtil.this是Activity,那么改成new AlertDialog.Builder(((Activity)HttpUtil.this));或者 new AlertDialog.Builder(getActivity());
解决方案二:
new AlertDialog.Builder(Context context) 不是需要HttpUtil.this
解决方案三:
HttpUtil是什么类?AlertDialog.Builder需要上下文对象来构造,改为:new AlertDialog.Builder(getActivity());,或者传递Context对象
解决方案四:
//1、得到对话框的构造器,用来构造对话框的模版
AlertDialog.Builder builder = new Builder(this);
//2、构造对话框的模版
//设置标题
builder.setTitle("提醒:");
//设置提示信息
builder.setMessage("若练此功,必先自宫!");
//设置确定按钮
builder.setPositiveButton("确定自宫", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "即使自宫,也未必成功", 0).show();
}
});
//设置取消按钮
builder.setNegativeButton("想想再说", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "若不自宫一定不会成功", 0).show();
}
});
//3、使用构造器创建一个对话框
AlertDialog dialog = builder.create();
//4、显示对话框
dialog.show();
时间: 2024-11-03 23:21:54