Android学习:不再提示对话框(创建自定义对话框)

来源:互联网 发布:c语言定义变量 编辑:程序博客网 时间:2024/06/05 20:47

界面:

创建自定义对话框首先应该定义对话框界面,我只是想要一个带有复选框的对话框界面,因此布局文件很简单:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <CheckBox        android:id="@+id/nomore"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:text="不再提示" /></LinearLayout>
接下来在应用程序中调用AlertDialog.Builder的setView(View view)方法让对话框显示该自定义界面即可。

final Builder builder=new AlertDialog.Builder(this);builder.setTitle("涉及系统权限,请您手动删除相应图片");LinearLayout linearLayout=(LinearLayout)getLayoutInflater().inflate(R.layout.nomore, null);builder.setView(linearLayout);builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {......}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {}});builder.create().show();}});

功能实现:将复选框的选择情况存储在SharedPreferences中,下次根据SharedPreferences中存储值判断是否弹出对话框。


原创粉丝点击