先上效果圖
//自定義style话瞧,讓彈窗引用
布局:寫彈窗樣式
效果圖:
布局代碼:
畫個(gè)Shape窄俏,下面布局代碼會用到,給個(gè)圓角顏色:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/shape_ui_alert_view" android:orientation="vertical" >
?<TextView android:id="@+id/tvTitle" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:text="標(biāo)題" android:textColor="@color/text_black" android:textSize="16sp" android:textStyle="bold" />
?<TextView android:id="@+id/tvMessage" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:text="內(nèi)容" android:textSize="16sp" />
?<View android:layout_width="match_parent"
?android:layout_height="0.5dp"
?android:background="@color/line_gray" />
?<LinearLayout
android:layout_width="match_parent"
?android:layout_height="40dp"
?android:orientation="horizontal" >
?<TextView
?android:id="@+id/tvBtnLeft"
android:layout_width="match_parent"
?android:layout_height="match_parent"
?android:layout_weight="1"
?android:gravity="center" android:text="取消" android:textSize="14sp" />
?<View
?android:layout_width="0.3dp"
?android:layout_height="match_parent"
?android:background="#E0E0E0" />
<TextView
android:id="@+id/tvBtnRight"
android:layout_width="match_parent"
?android:layout_height="match_parent"
?android:layout_weight="1"
android:gravity="center" android:text="確定" android:textSize="14sp" />
?</LinearLayout>
//自定義類繼承Dialog
public class UIAlertViewextends Dialog {
private Contextcontext;
private Stringtitle;
private Stringmessage;
private StringbuttonLeftText;
private StringbuttonRightText;
private ClickListenerInterfaceclickListenerInterface;
public UIAlertView(Context context, String title, String message,
String buttonLeftText, String buttonRightText) {
super(context, R.style.UIAlertViewStyle);
this.context = context;
this.title = title;
this.message = message;
this.buttonLeftText = buttonLeftText;
this.buttonRightText = buttonRightText;
}
public interface ClickListenerInterface {
public void doLeft();
public void doRight();
}
@Override
? ? protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inite();
}
public void inite() {
LayoutInflater inflater = LayoutInflater.from(context);
//這里是加載布局颂跨,上面已經(jīng)也好了
View view = inflater.inflate(R.layout.ui_alert_view,null);
setContentView(view);
//獲取布局中的ID
TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);
TextView tvLeft = (TextView) view.findViewById(R.id.tvBtnLeft);
TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);
TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
if ("".equals(title)) {
tvTitle.setVisibility(View.GONE);
}else {
tvTitle.setText(title);
}
tvMessage.setText(message);
tvLeft.setText(buttonLeftText);
tvRight.setText(buttonRightText);
tvLeft.setOnClickListener(new clickListener());
tvRight.setOnClickListener(new clickListener());
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d =context.getResources().getDisplayMetrics();
lp.width = (int) (d.widthPixels *0.8);
dialogWindow.setAttributes(lp);
}
public void setClicklistener(ClickListenerInterface clickListenerInterface) {
this.clickListenerInterface = clickListenerInterface;
}
private class clickListenerimplements View.OnClickListener {
@Override
? ? ? ? public void onClick(View v) {
int id = v.getId();
switch (id) {
//左邊的點(diǎn)擊事件
case R.id.tvBtnLeft:
clickListenerInterface.doLeft();
break;
//右邊的點(diǎn)擊事件
case R.id.tvBtnRight:
clickListenerInterface.doRight();
break;
default:
break;
}
}
}
;
}
這里是具體使用
final UIAlertView delDialog =new UIAlertView(mContext,"溫馨提示","確認(rèn)刪除該商品嗎?",
"取消","確定");
delDialog.show();
delDialog.setClicklistener(new UIAlertView.ClickListenerInterface() {
@Override
public void doLeft() {
//左邊按鈕的點(diǎn)擊事件 //.dismiss()消失彈窗
delDialog.dismiss();
}
//有點(diǎn)按鈕的點(diǎn)擊事件
@Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void doRight() {
delDialog.dismiss();
}
}
);