我有一个警报类,我用我这个表格中的方法定义我的应用程序的所有警报窗口
public static void Alert1(Context con) {
AlertDialog.Builder builder = new AlertDialog.Builder(con);
builder.setTitle("my title");
builder.setIcon(android.R.drawable.ic_dialog_info);
DialogListner listner = new DialogListner();
builder.setMessage("my message");
builder.setPositiveButton("ok", listner);
AlertDialog diag = builder.create();
diag.show();
}
现在我想以类似的方式创建一个带有复选框的警告窗口 "不要再显示这个" 如果被选中以避免查看此警报
答案 0 :(得分:3)
我对这个问题有一个明确和正确的方法
package com.example.user.testing;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {
CheckBox dontShowAgain;
public static final String PREFS_NAME = "MyPrefsFile1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
LayoutInflater adbInflater = LayoutInflater.from(MainActivity.this);
View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
adb.setView(eulaLayout);
adb.setTitle("Attention");
adb.setMessage("Your message here");
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("skipMessage", dontShowAgain.isChecked());
editor.commit();
dialog.cancel();
}
});
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
Boolean skipMessage = settings.getBoolean("skipMessage", false);
if (skipMessage.equals(false)) {
adb.show();
}
}
}
答案 1 :(得分:1)
您可以使用应用程序SharedPreference
对象。通过尝试首次阅读,在您的共享首选项中添加项目。您所要做的就是为对象添加一个值,并且每次都检查它。
答案 2 :(得分:0)
您可以使用此复选框和textview为对话框创建单独的布局。对此布局进行充气并将其设置为对话框的视图
好吧,它会像下面这样。View view = layoutInflator.inflate(R.layout.dialogView,null);
//and set to your dialog
dialog.setView(view);
//Or you can set it to your builder also
处理onCheckListener以获取复选框
答案 3 :(得分:0)
正如其他人所建议的那样,您应该使用所需的消息和控件(复选框)创建自定义布局,并使用SharedPreferences
来跟踪用户选择
以下是2篇描述这些主题的博客文章
Creating and Displaying a Custom Dialog in Android
Writing and Reading from SharedPreferences
(免责声明:我的博客。)