我正在做如下所示的自定义AlertDialog:
public class Message {
private static AlertDialog.Builder alertDialog;
private final static Handler m_handler = new Handler() {
@Override
public void handleMessage(Message mesg) {
throw new RuntimeException();
}
};
public static void simpleMessage(String Message, String textButton, Context act){
alertDialog = new AlertDialog.Builder(act);
LayoutInflater inflater = (LayoutInflater) act.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View v = inflater.inflate(R.layout.message_simple,null);
TextView tvMessageSimple = (TextView) v.findViewById(R.id.tv_message_simple);
Button btnOKMessageSimple = (Button) v.findViewById(R.id.btn_ok_message_simple);
tvMessageSimple.setText(message);
btnOKMessageSimple.setText(textButton);
alertDialog.setView(v);
alertDialog.show();
btnOKMessageSimple.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
m_handler.sendMessage(m_handler.obtainMessage());
}
});
// loop till a runtime exception is triggered.
try {
Looper.loop();
}
catch(RuntimeException e2) {
;
}
}
}
AlertDialog非常有效,当按下按钮时,它应该完成AlertDialog。
按下按钮时,执行catch(RuntimeException e2),但AlertDialog结束。
我该如何完成?
感谢。
答案 0 :(得分:1)
构建警告对话框时,应保存引用,以便在使用Dialog#dismiss()后将其关闭。方法
看看这段代码:
final AlertDialog dialog = alertDialog.show();
btnOKMessageSimple.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
m_handler.sendMessage(m_handler.obtainMessage());
}
});
最后摆脱try / catch。
您还可以参考this question了解更多信息。