«

AlertDialog自定义布局

时间:2024-3-2 17:24     作者:韩俊     分类: Android


AlertDialog自定义布局

activity调用

private void showAlertDialog() {

final AlertDialogUtil dialog = new AlertDialogUtil(this, false, null); // false代表必须点击"确定"其它不可以点击不消失,true点击其它也消失

dialog.setMessage("您尚未登录");

dialog.setBtnPositiveValue("确定");

dialog.setPositiveClickListener(new OnClickListener() {



@Override

public void onClick(View arg0) {

dialog.dismiss();

}

});

dialog.setBtnNegativeValue("取消");

dialog.setNegativeClickListener(new OnClickListener() {



@Override

public void onClick(View v) {

// TODO Auto-generated method stub



}

});

dialog.show();

}


AlertDialogUtil工具类

public class AlertDialogUtil extends AlertDialog {

private Button btnPositive, btnNegative;
private TextView tvMessage;

private String message;
private String btnPositiveValue, btnNegativeValue;
private View.OnClickListener positiveListener, negativeListener;

public AlertDialogUtil(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_confirm);
initView();
}

public void setMessage(String message) {
this.message = message;
}

public void setBtnPositiveValue(String val) {
this.btnPositiveValue = val;
}

public void setPositiveClickListener(View.OnClickListener listener) {
this.positiveListener = listener;
}

public void setBtnNegativeValue(String val) {
this.btnNegativeValue = val;
}

public void setNegativeClickListener(View.OnClickListener listener) {
this.negativeListener = listener;
}

private void initView() { // 布局 你可以写你想要的对话框布局
btnPositive = (Button) findViewById(R.id.btn_alertDialogutil_cancel);
btnNegative = (Button) findViewById(R.id.btn_alertDialogutil_confirm);
tvMessage = (TextView) findViewById(R.id.textView_title_mydialog);

if (isNullEmptyBlank(message)) {
tvMessage.setVisibility(View.GONE);
} else {
this.setCancelable(cancelable);
tvMessage.setText(message);
}

if (!(isNullEmptyBlank(btnPositiveValue))) {
btnPositive.setText(btnPositiveValue);
this.btnPositive.setOnClickListener(positiveListener);
}

if (!(isNullEmptyBlank(btnNegativeValue))) {
btnNegative.setText(btnNegativeValue);
this.btnNegative.setOnClickListener(negativeListener);
}

}

private static boolean isNullEmptyBlank(String str) {
if (str == null || "".equals(str) || "".equals(str.trim()))
return true;
return false;
}

}


xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="260dp"
android:layout_height="130dp"
android:layout_gravity="center"
android:background="@drawable/feedback_edite_bg"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" >

<TextView
android:id="@+id/textView_title_mydialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/color_black"
android:textSize="21sp" >
</TextView>
</LinearLayout>

<LinearLayout
android:id="@+id/ll_alertDialogutil"
android:layout_width="match_parent"
android:layout_height="51dp"
android:orientation="vertical"
android:visibility="gone" >

<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_linear" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="horizontal" >

<Button
android:id="@+id/btn_alertDialogutil_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/color_transparent"
android:textColor="@color/color_blue"
android:textSize="15sp"
android:visibility="gone" />

<ImageView
android:id="@+id/iv_alertDialogutil"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/color_linear"
android:visibility="gone" />

<Button
android:id="@+id/btn_alertDialogutil_confirm"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/color_transparent"
android:textColor="@color/color_blue"
android:textSize="15sp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

标签: android

热门推荐