首页 >  Android开发 >  正文

Android 点按钮不让AlertDialog退出方法


https://www.panxw.com    栏目:Android开发    标签: AlertDialog     日期:2011-11-10    阅读:

在AlertDialog中加入布局后,发现数据验证无论通过与否,点了AlertDialog提供的按钮后总是退出。如何使数据在校验未成功的时候hold住AlertDialog呢?网友提供了一种可行方法:

final Builder dialog = new AlertDialog.Builder(mContext);
...
LayoutInflater inflater = LayoutInflater.from(mContext);
View layout = inflater.inflate(R.layout.tsm_person_info, null);
final EditText nameText = (EditText) layout.findViewById(R.id.editText1);
final EditText cardIdText = (EditText) layout.findViewById(R.id.editText2);
final EditText phoneText = (EditText) layout.findViewById(R.id.editText3);
... 
dialog.setView(layout);
dialog.setNeutralButton("确定", new DialogInterface.OnClickListener() {
  @Override
 public void onClick(DialogInterface dialog, int which) { 
  String name = nameText.getText().toString().trim();
  String idCard = cardIdText.getText().toString().trim();
  String phone = phoneText.getText().toString().trim();
  if (name.equals("") || idCard.equals("") || phone.equals("")) {
  holdDialog(dialog, false); //校验不通过界面保持
  return;
 }
 holdDialog(dialog, true); //allow exit
 }
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
  Toast.makeText(mContext, "未进行个人化处理!", Toast.LENGTH_SHORT).show();
  holdDialog(dialog, true);//allow exit
 }
});
/**
 *界面保持开关
* @param dialog
*/
private void holdDialog(DialogInterface dialog, boolean flag) {
 try {
  Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
  field.setAccessible(true);
  field.set(dialog, flag);
 } catch (Exception e) {
  e.printStackTrace();
 }
}

参考文章:http://gjican.iteye.com/blog/1130538

本文为原创文章,转载请注明出处:Panxw's Blog » Android 点按钮不让AlertDialog退出方法
如果本站内容帮助到了你,请记得收藏、分享,或打赏支持,谢谢!

上篇: Windows下eclipse中添加NDK Builder