自定义AlertDialog

UiHelper.java文件:

package com.show.act;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UiHelper {

/** 点击确定后触发的事件. */
public interface OnClickYesListener {
abstract void onClickYes();
}

/** 点击取消后触发的事件. */
public interface OnClickNoListener {
abstract void onClickNo();
}

public static void showQuestionDialog(Context context, String title,
String text, final OnClickYesListener listenerYes,
final OnClickNoListener listenerNo) {

Builder builder = new AlertDialog.Builder(context);

if (!isBlank(text)) {
final TextView textView = new TextView(context);
textView.setText(text);
LinearLayout layout = new LinearLayout(context);
layout.setPadding(10, 0, 10, 0);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final ImageView mImgView = new ImageView(context);
mImgView.setBackgroundResource(R.drawable.icon);
layout.addView(mImgView, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
builder.setView(layout);
}
builder.setTitle(title);
builder.setPositiveButton(context.getString(R.string.yes),
new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (listenerYes != null) {
listenerYes.onClickYes();
}
}
});
builder.setNegativeButton(context.getString(R.string.no),
new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (listenerNo != null) {
listenerNo.onClickNo();
}
}
});
builder.setCancelable(true);
builder.create().show();

}

/** 判断显示内容是否为null. */
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
}



在代码中,我们只要这样调用即可:

mButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
UiHelper.showQuestionDialog(ShowDialogActivity.this, "111",
"222", new OnClickYesListener() {
public void onClickYes() {
}
}, new OnClickNoListener() {
public void onClickNo() {
}
});
}
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的自定义AlertDialog布局示例,其中包含一个标题、一个消息、两个按钮: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/alert_dialog_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是自定义AlertDialog" android:textSize="18sp" android:textColor="@color/colorPrimary"/> <TextView android:id="@+id/alert_dialog_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="这是自定义AlertDialog的消息内容" android:textSize="16sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="16dp"> <Button android:id="@+id/alert_dialog_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消"/> <Button android:id="@+id/alert_dialog_confirm" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定"/> </LinearLayout> </LinearLayout> ``` 这个布局包含一个LinearLayout,其中包含一个标题和一个消息的TextView,以及两个按钮的LinearLayout。注意,按钮使用了layout_weight属性,让它们均分父布局的宽度。在代码中,你可以通过findViewById()方法找到这些控件,设置它们的文本、图标、事件监听器等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值