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() {
}
});
}
});