一、AlertDialog的使用和接口回调的使用
1、我们先写一个MyDialog类
public class MyDialog {
private static MyDialog mMyDialog;
private AlertDialog dialog = null;
private MyDialog(){
}
//单例模式使用
public static MyDialog getInstance(){
if(null == mMyDialog){
mMyDialog = new MyDialog();
}
return mMyDialog;
}
public void showMyDialog(final Context mContext) {
if (null == dialog) {
dialog = new AlertDialog.Builder(mContext).create();
dialog.setTitle(R.string.unmount_tips);
dialog.setMessage(mContext.getText(R.string.unmount_clear_tips));
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, mContext.getText(R.string.stop_record_confirm),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(null != listener){
listener.onConfirmed();
}
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mContext.getText(R.string.stop_record_cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(null != listener){
listener.onCanceled();
}
hideDialog();
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface d) {
dialog = null;
}
});
//8.0系统加强后台管理,禁止在其他应用和窗口弹提醒弹窗,如果要弹,必须使用TYPE_APPLICATION_OVERLAY
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0新特性
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY - 1);
} else {
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
}
//dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
dialog.show();
} else {
if (!dialog.isShowing()) {
dialog.show();
}
}
}
public void hideDialog() {
if (null != dialog && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
}
//接口回调的使用
private CallBack listener = null;
public void setListener(CallBack listener){
this.listener = listener;
}
public static interface CallBack{
void onConfirmed();
void onCanceled();
}
}
2、在布局中定义一个Button
<?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"
>
<Button
android:id="@+id/DialogOnclick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DialogOnclick"
android:onClick="DialogOnclick"
/>
</RelativeLayout>
3、使用MyDialog
public class MainActivity extends AppCompatActivity implements MyDialog.CallBack {
private static final String TAG = "MainActivity";
private MyDialog mMyDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(null == mMyDialog){
mMyDialog = MyDialog.getInstance();
}
}
public void DialogOnclick(View view) {
Log.d(TAG, "DialogOnclick()");
mMyDialog.setListener(this);
mMyDialog.showMyDialog(this);
}
@Override
public void onConfirmed() {
Log.d(TAG, "onConfirmed()");
}
@Override
public void onCanceled() {
Log.d(TAG, "onCanceled()");
}
}
4、效果图如下