publicclass Alert extends AlertDialog implements OnContentListener {
privatestaticfinalintRES = R.layout.alert;
private AlertDialog alert;
private TextView messageView;
private LinearLayout buttonBoxLayout;
public Alert(AlertDialog alert, Context context) {
super(context);
this.alert = alert;
}
@Override
publicvoid onContent(Window window) {
messageView = (TextView) window.findViewById(R.id.message);
buttonBoxLayout = (LinearLayout) window.findViewById(R.id.layout_button_box);
}
publicvoid setMessage(int resId) {
messageView.setText(resId);
}
publicvoid setMessage(String message) {
messageView.setText(message);
}
publicvoid setPositiveButton(String text, final View.OnClickListener listener) {
Context context = getContext();
TextView button = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
button.setLayoutParams(params);
button.setText(text);
button.setTextColor(Color.BLACK);
button.setTextSize(20);
button.setGravity(Gravity.CENTER);
button.setOnClickListener(listener);
buttonBoxLayout.addView(button);
}
publicvoid setNegativeButton(String text, final View.OnClickListener listener) {
Context context = getContext();
TextView button = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
button.setLayoutParams(params);
button.setText(text);
button.setTextColor(Color.BLACK);
button.setTextSize(20);
button.setGravity(Gravity.CENTER);
button.setOnClickListener(listener);
if(buttonBoxLayout.getChildCount() > 0) {
buttonBoxLayout.addView(button, 1);
} else {
button.setLayoutParams(params);
buttonBoxLayout.addView(button);
}
}
publicvoid show() {
show(RES, null);
}
publicvoid show(int res) {
show(res, null);
}
publicvoid show(int res, OnContentListener listener) {
alert.show();
Window window = alert.getWindow();
window.setContentView(res);
listener = listener == null ? this : listener;
listener.onContent(window);
}
publicvoid dismiss() {
alert.dismiss();
}
publicstaticclass Builder {
private AlertDialog.Builder innerBuilder;
public Builder(Context context) {
innerBuilder = new AlertDialog.Builder(context);
}
public Alert create() {
AlertDialog alert = innerBuilder.create();
Alert newalert = new Alert(alert, alert.getContext());
return newalert;
}
}
}
publicinterface OnContentListener {
publicvoid onContent(Window window);
}
final Alert alert = new Alert.Builder(activity).create();
alert.show();
alert.setMessage("请输入用户名");
alert.setPositiveButton("确定", new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
alert.dismiss();
Toast.makeText(activity.getApplicationContext(), "被点到确定", Toast.LENGTH_LONG).show();
}
});
alert.setNegativeButton("取消", new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
alert.dismiss();
Toast.makeText(activity.getApplicationContext(), "被点到取消", Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = new AlertDialog.Builder(activity).create();
final Alert alert = new Alert(dialog, activity);
alert.show();
alert.setMessage("请输入用户名");
alert.setPositiveButton("确定", new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
alert.dismiss();
Toast.makeText(activity.getApplicationContext(), "被点到确定", Toast.LENGTH_LONG).show();
}
});
alert.setNegativeButton("取消", new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
alert.dismiss();
Toast.makeText(activity.getApplicationContext(), "被点到取消", Toast.LENGTH_LONG).show();
}
});