package com.cfae.cfaeapp.widget; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.TextView; import com.cfae.cfaeapp.R; /** * Created by 郝悅 on 2018/6/25. */ public class CommomDialog extends Dialog implements View.OnClickListener { private TextView contentTxt; private TextView titleTxt; private TextView submitTxt; private TextView cancelTxt; private Context mContext; private String content; private OnCloseListener listener; private String positiveName; private String negativeName; private String title; public CommomDialog(Context context) { super(context); this.mContext = context; } public CommomDialog(Context context, String content) { super(context, R.style.MyDialog); this.mContext = context; this.content = content; } public CommomDialog(Context context, int themeResId, String content) { super(context, themeResId); this.mContext = context; this.content = content; } public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener) { super(context, themeResId); this.mContext = context; this.content = content; this.listener = listener; } protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); this.mContext = context; } public CommomDialog setTitle(String title) { this.title = title; return this; } public CommomDialog setPositiveButton(String name) { this.positiveName = name; return this; } public CommomDialog setNegativeButton(String name) { this.negativeName = name; return this; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.view_quit_dialog); setCanceledOnTouchOutside(false); initView(); } private void initView() { contentTxt = findViewById(R.id.message); titleTxt = findViewById(R.id.title); submitTxt = findViewById(R.id.submit); submitTxt.setOnClickListener(this); cancelTxt = findViewById(R.id.cancel); cancelTxt.setOnClickListener(this); contentTxt.setText(content); if (!TextUtils.isEmpty(positiveName)) { submitTxt.setText(positiveName); } if (!TextUtils.isEmpty(negativeName)) { cancelTxt.setText(negativeName); } if (!TextUtils.isEmpty(title)) { titleTxt.setText(title); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.cancel: if (listener != null) { listener.onClick(this, false); } this.dismiss(); break; case R.id.submit: if (listener != null) { listener.onClick(this, true); } break; } } public interface OnCloseListener { void onClick(Dialog dialog, boolean confirm); } }
style:java
<!--自定義Dialog--> <style name="MyDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <!--邊框--> <item name="android:windowIsFloating">true</item> <!--是否浮如今activity之上--> <item name="android:windowIsTranslucent">false</item> <!--半透明--> <item name="android:windowNoTitle">true</item> <!--無標題--> <item name="android:windowBackground">@android:color/transparent</item> <!--背景透明--> <item name="android:backgroundDimEnabled">true</item> <!--模糊-->
佈局:android
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="280dp" android:layout_height="145dp" android:layout_centerInParent="true" android:background="@drawable/shape_quit_dialog" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="23.5dp" android:gravity="center" android:text="@string/friendly_reminder" android:textColor="#4A4A4A" android:textSize="18sp" /> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="6.5dp" android:gravity="center" android:text="確認要退出?" android:textColor="#7C7C7D" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginTop="23.5dp" android:background="#EEEEEE" /> <LinearLayout android:layout_width="match_parent" android:layout_height="45.5dp" android:orientation="horizontal"> <TextView android:id="@+id/cancel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="取消" android:textColor="@drawable/shape_quit_dialog_font_color" android:textSize="18sp" /> <View android:layout_width="0.5dp" android:layout_height="match_parent" android:background="#EEEEEE" /> <TextView android:id="@+id/submit" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="肯定" android:textColor="#DDA944" android:textSize="18sp" /> </LinearLayout> </LinearLayout>
java代碼:app
/** * 顯示對話框 * @param context 上下文 */ public static void showLoginDialog(final Context context) { if (context == null) { LogUtils.w(TAG, "沒法顯示登陸對話框,由於傳入上下文爲空!"); return; } new CommomDialog(context, R.style.MyDialog, "您還未登陸或登陸已超時,請先登陸", new CommomDialog.OnCloseListener() { @Override public void onClick(Dialog dialog, boolean confirm) { if (confirm == true) { //跳轉到登陸 dialog.dismiss(); context.startActivity(new Intent(context, LoginActivity.class)); } if (confirm == false) { dialog.dismiss(); } } }).setTitle("友情提示").show(); }