很多app在启动时,都会弹框提示一些条款,用户可以勾选复选框,点击确定,该弹框以后不会再出现,也可以点击取消,下次启动app时该弹框还会出现。下面给出一个实现该功能的demo,具体如下:
主界面的布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.firstexplaindialog.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Hello World!"
android:textSize="16sp" />
</RelativeLayout>
弹框的布局文件activity_popup_window.xml
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="欢迎"
android:textColor="#ff1d9f8c"
android:textSize="16sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#ff999999"
android:contentDescription="@null"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="20dp"
android:text="勾选checkbox点击确定后,不再弹框;\n否则每次启动都会弹框"
android:textSize="14sp" />
<LinearLayout
android:id="@+id/zas_first_explain_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp">
<CheckBox
android:id="@+id/never_appear_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_marginLeft="6dp"
android:gravity="center_vertical"
android:text="不再出现"
android:textColor="#ff333333"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="36dp"
android:gravity="center"
android:background="#ffededed"
android:layout_marginBottom="10dp">
<TextView
android:id="@+id/never_appear_cancel_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="取消" />
<ImageView
android:layout_width="1dp"
android:layout_height="30dp"
android:background="#ff999999"
android:contentDescription="@null"
android:visibility="visible"/>
<TextView
android:id="@+id/never_appear_confirm_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="确定" />
</LinearLayout>
</LinearLayout>
主界面的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();
}
}