带复选框的Alertdialog(不再显示)

时间:2014-02-28 08:51:27

标签: java android checkbox dialog

我需要显示一个带有“不再显示”复选框的AlertDialog。我搜索过,但找不到合适的解决方案:/

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
PackageManager pk = getPackageManager();
Drawable icon;

alertDialogBuilder
    .setTitle(R.string.confirm)
    .setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            //Do something
        }
    });

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

3 个答案:

答案 0 :(得分:1)

如果我没有错,那么只需使用 查看 来扩展一个课程。

public class DialogShow extends View {
SharedPreferences dialogPreferences;
String prefrencestring;
CheckBox nevershowagain;
Button closedialog;
Dialog dialog;
View view;

public DialogShow(final Context context) {
    super(context);
    dialog = new Dialog(context);
    view = View.inflate(context, R.layout.startdialog, null);
    dialog.setContentView(view);
    nevershowagain = (CheckBox) view.findViewById(R.id.nevershowagain);
    closedialog = (Button) view.findViewById(R.id.closedialog);

    closedialog.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (nevershowagain.isChecked()) {
                prefrencestring = "1";

                dialogPreferences = PreferenceManager
                        .getDefaultSharedPreferences(context);
                Editor editprefrences = dialogPreferences.edit();
                editprefrences.putString("showdialog", prefrencestring);
                editprefrences.commit();
            }

            dialog.dismiss();
        }
    });

    dialogPreferences = PreferenceManager
            .getDefaultSharedPreferences(context);
    String check = dialogPreferences.getString("showdialog", "");
    if (check.equals("1")) {
    } else {
        dialog.show();
    }
}

}

现在在onCreate()方法的启动活动中调用此类..

     DialogShow  d = new Dialog(this);

答案 1 :(得分:1)

您可以尝试使用AlertDialog:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();

builder.setView(inflater.inflate(R.layout.myDialogLayout, null));

Dialog d = builder.create();

修改:请查看HERE FOR DETAILS EXPLANATION

答案 2 :(得分:0)

将您的视图传递给setView()方法,它会将您的视图设置为对话框主体。

alertDialogBuilder.setView(your_view);