import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.ahqld.exam_assistant_app.R;
import com.ahqld.exam_assistant_app.config.appConfigKey;
import com.ahqld.exam_assistant_app.myview.SlideButton;
import com.ahqld.exam_assistant_app.util.MySharedPreferences;
import static android.graphics.Color.parseColor;
public class PracticeSettingDialog2 {
private Context context;
private Dialog dialog;
private Display display;
private LinearLayout lLayout_bg;
public SlideButton bt1,bt2;
public View view;
private Back back;
public PracticeSettingDialog2(Context context,Back back) {
this.context=context;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
display = windowManager.getDefaultDisplay();
this.back=back;
}
public PracticeSettingDialog2 builder() {
// 獲取Dialog布局
view = LayoutInflater.from(context).inflate(R.layout.practice_setting_dialog, null);
lLayout_bg = view.findViewById(R.id.root);
// 定義Dialog布局和參數(shù)
dialog = new Dialog(context, R.style.AlertDialogStyle);
dialog.setContentView(view);
// int matchParent = ViewGroup.LayoutParams.MATCH_PARENT;//父布局的寬度
Window dialogWindow = dialog.getWindow();
dialogWindow.setBackgroundDrawableResource(android.R.color.white);
dialogWindow.setGravity(Gravity.BOTTOM | Gravity.CENTER);
// 調(diào)整dialog背景大小
lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth()), WindowManager.LayoutParams.WRAP_CONTENT));
init();
return this;
}
private void init(){
bt1=view.findViewById(R.id.bt1);
bt1.setSmallCircleModel(parseColor("#cccccc"), parseColor("#00000000"), parseColor("#FF2196F3"), parseColor("#cccccc"));
bt1.setOnCheckedListener(new SlideButton.SlideButtonOnCheckedListener() {
@Override
public void onCheckedChangeListener(boolean isChecked) {
MySharedPreferences.setValue(context, appConfigKey.autoNext,isChecked+"");
back.back();
}
});
String autoNext = MySharedPreferences.getValue(context, appConfigKey.autoNext);
System.out.println("autoNext="+autoNext);
if(autoNext.equals("true") || autoNext.equals("")){
bt1.setChecked(true);
}else {
bt1.setChecked(false);
}
bt2=view.findViewById(R.id.bt2);
bt2.setSmallCircleModel(parseColor("#cccccc"), parseColor("#00000000"), parseColor("#FF2196F3"), parseColor("#cccccc"));
bt2.setOnCheckedListener(new SlideButton.SlideButtonOnCheckedListener() {
@Override
public void onCheckedChangeListener(boolean isChecked) {
MySharedPreferences.setValue(context,appConfigKey.sound,isChecked+"");
back.back();
}
});
String sound = MySharedPreferences.getValue(context, appConfigKey.sound);
System.out.println("sound="+sound);
if(sound.equals("true")||sound.equals("")){
bt2.setChecked(true);
}else {
bt2.setChecked(false);
}
}
public interface Back{
void back();
}
public void show() {
dialog.show();
}
}
布局文件 view_alertdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lLayout_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aaa"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:textColor="@color/black"
android:textSize="16sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:background="@color/alertdialog_line" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_neg"
android:layout_width="wrap_content"
android:layout_height="43dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/actionsheet_blue"
android:textSize="16sp" />
<ImageView
android:id="@+id/img_line"
android:layout_width="0.5dp"
android:layout_height="43dp"
android:background="@color/alertdialog_line" />
<Button
android:id="@+id/btn_pos"
android:layout_width="wrap_content"
android:layout_height="43dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/actionsheet_blue"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
調(diào)用 和調(diào)用Dialog一樣的
new IosAlertDialog(this).builder().setTitle("退出當(dāng)前賬號")
.setMsg("再連續(xù)登陸15天牙丽,就可變身為QQ達(dá)人兔魂。退出QQ可能會使你現(xiàn)有記錄歸零,確定退出构罗?")
.setPositiveButton("確認(rèn)退出", new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}).setNegativeButton("取消", new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}).show();