条款弹框提示实现

很多app在启动时,都会弹框提示一些条款,用户可以勾选复选框,点击确定,该弹框以后不会再出现,也可以点击取消,下次启动app时该弹框还会出现。下面给出一个实现该功能的demo,具体如下:


主界面的布局文件activity_main.xml




    


弹框的布局文件activity_popup_window.xml




    

    

    

    

        

        
    

    

        
        

        
    


主界面的java文件MainActivity.java。

弹框需要延时加载,使用getwindow().getDecorView().post()方式对于不同配置的机器有更好的适配;可以参考:http://www.open-open.com/lib/view/open1447940095744.html

将数据存入SharedPreferences中,做成一个类进行封装,有利于后期扩展。

package com.example.firstexplaindialog;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.firstexplaindialog.db.SharedPre;

public class MainActivity extends AppCompatActivity {

    private PopupWindow mPopupWindow = null;
    private boolean isNeverAppear = false;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPre.init(this);

        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        if (!SharedPre.getIsNeverAppear()) {
                            try {
                                initPopupWindow();
                                showPopupWindow();
                            } catch (Exception e) {
                            }
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
            mPopupWindow = null;
        }

        if (handler != null) {
            handler = null;
        }
    }

    private void initPopupWindow() {
        CheckBox never_appear_checkbox;
        TextView never_appear_cancel_tv;
        TextView never_appear_confirm_tv;

        LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View popup_window = mLayoutInflater.inflate(R.layout.activity_popup_window, null);
        mPopupWindow = new PopupWindow(popup_window, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        never_appear_checkbox = (CheckBox) popup_window.findViewById(R.id.never_appear_checkbox);
        never_appear_cancel_tv = (TextView) popup_window.findViewById(R.id.never_appear_cancel_tv);
        never_appear_confirm_tv = (TextView) popup_window.findViewById(R.id.never_appear_confirm_tv);

        never_appear_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isNeverAppear = isChecked;
            }
        });

        never_appear_cancel_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                mPopupWindow.dismiss();
            }
        });

        never_appear_confirm_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPre.setIsNeverAppear(isNeverAppear);
                mPopupWindow.dismiss();
            }
        });
    }

    private void showPopupWindow() {
        View rootView = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        mPopupWindow.showAtLocation(rootView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}

SharedPre.java

package com.example.firstexplaindialog.db;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by jh on 2016/1/20.
 */
public class SharedPre {
    private static Context mContext = null ;
    private static SharedPreferences preferences = null;

    private static final String isNeverAppear = "isNeverAppear";

    public static void init(Context context){
        mContext = context ;
        preferences = mContext.getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
    }

    public static boolean getIsNeverAppear() {
        return preferences.getBoolean(isNeverAppear, false);
    }

    public static void setIsNeverAppear(boolean isNeverAppear) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(SharedPre.isNeverAppear, isNeverAppear);
        editor.commit();
    }
}


你可能感兴趣的:(android,ui)