1. 程式人生 > >Android 不再提醒對話方塊

Android 不再提醒對話方塊

app 在首次進入某個介面的時候,有提示需求彈出對話方塊。如果點選不再提示,下次進去的時候。對話方塊將 不再彈出。
通用提示框
**Dialog
 */
public class ApproveDialog extends Dialog implements View.OnClickListener {

    // private Context context;
    private String titleStr;
    private String contentStr;
    private String cancelStr;
    private String goStr;

    public interface ApproveDialogHelper {
        void go();

        void cancel();
    }

    private ApproveDialogHelper mConfirmDialogHelper;

    /**
     * 新建時 , 用這個建構函式
     */
    public ApproveDialog(Context context, String titleStr, String contentStr, String goStr, String tv_cancle_authentication, ApproveDialogHelper helper) {
        super(context, R.style.CustomDialog);
        this.titleStr = titleStr;
        this.contentStr = contentStr;
        this.goStr = goStr;
        this.cancelStr = tv_cancle_authentication;
        this.mConfirmDialogHelper = helper;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.authentication_dialog);
        setCanceledOnTouchOutside(true);
        Window window = getWindow();// 獲得視窗
        WindowManager manager = window.getWindowManager();// 獲得管理器
        Display display = manager.getDefaultDisplay();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = (int) (display.getWidth() * 0.8);
        window.setAttributes(attributes);
        init();
    }

    private void init() {
        TextView tv_title = (TextView) findViewById(R.id.authentication_dialog_title);
        TextView tv_content = (TextView) findViewById(R.id.authentication_dialog_content);
        TextView tv_go = (TextView) findViewById(R.id.tv_authentication);
        TextView tv_cancle_authentication = (TextView) findViewById(R.id.tv_cancle_authentication);

        tv_title.setText(titleStr);
        tv_content.setText(contentStr);
        tv_go.setText(goStr);
        tv_cancle_authentication.setText(cancelStr);

        tv_go.setOnClickListener(this);
        tv_cancle_authentication.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_authentication:
                // 確定
                if (mConfirmDialogHelper != null) {
                    mConfirmDialogHelper.go();
                }
                dismiss();
                break;
            case R.id.tv_cancle_authentication:
                // 取消
                if (mConfirmDialogHelper != null) {
                    mConfirmDialogHelper.cancel();
                }
                dismiss();
                break;
            default:
                break;
        }
    }
}

介面:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/approve_dialog_style"
        android:minHeight="100dp"
        android:orientation="vertical"
        android:padding="20dp">

        <TextView
            android:id="@+id/authentication_dialog_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:gravity="center_horizontal"
            android:text="標題"
            android:textColor="@color/gray_66"
            android:textSize="18sp"
            android:textStyle="bold" />

        <View style="@style/line_horizontal_gray" />

        <TextView
            android:id="@+id/authentication_dialog_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="left"
            android:padding="5dp"
            android:singleLine="false"
            android:text="提示內容"
            android:textColor="@color/lightblack"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal">


            <TextView
                android:id="@+id/tv_authentication"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_gravity="center"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:background="@drawable/login_style"
                android:gravity="center"
                android:lines="1"
                android:text="提交"
                android:textColor="@color/bgcolor" />

            <TextView
                android:id="@+id/tv_cancle_authentication"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:layout_weight="1"
                android:background="@drawable/shape_both_gray_gray"
                android:gravity="center"
                android:lines="1"
                android:text="取消"
                android:textColor="@color/bgcolor" />

        </LinearLayout>


    </LinearLayout>

</RelativeLayout>
  <style name="CustomDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
    </style>

應用:

ApproveDialog dialog;

  //判斷接受到的資訊
        if (value.endsWith("1")) {
            //不顯示
        } else {
            IsshowDialog(getActivity());
        }

 // 是否顯示提示框
    @SuppressLint("ApplySharedPref")
    public void IsshowDialog(final Context context) {
        dialog = new ApproveDialog(context, "重要提示", "為在一定範圍內保證大家使用安全,新版供求釋出的發帖人必須至少完成手機認證和身份認證。\n但汽修寶典目前只提供資訊釋出,請求職者仔細辨別招聘資訊並保證隱私安全。", "同意", "同意並且不再提示", new ApproveDialog.ApproveDialogHelper() {

            @Override
            public void go() {
                dialog.dismiss();
                //將資料儲存到sharedPerferences中:
                SharedPreferences pre = getActivity().getSharedPreferences("check_buy", MODE_PRIVATE);
                SharedPreferences.Editor editor = pre.edit();
                editor.putString("is_buy", "0");
                //提交選擇的check,並且儲存在pre中
                editor.commit();
            }

            @Override
            public void cancel() {
                dialog.dismiss();
                //將資料儲存到sharedPerferences中:
                SharedPreferences pre = getActivity().getSharedPreferences("check_buy", MODE_PRIVATE);
                SharedPreferences.Editor editor = pre.edit();
                editor.putString("is_buy", "1");
                //提交選擇的check,並且儲存在pre中
                editor.commit();
            }
        });
        dialog.show();
    }