AlertDialog.Builder对话框没有类似finish()或者dismiss()这样的方法。
但是它的父类AlertDialog有dismiss方法,而且AlertDialog.Builder在.show()的时候会得到一个AlertDialog对象,我们就可以用dismiss方法将该Builder关闭。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog dialog = builder.show();
dialog.dismiss();
DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
try {
//不关闭
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, false);
} catch (Exception e) {
e.printStackTrace();
}
if(inputCheck()){
//关闭对话框
try {
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
} catch (Exception e) {
e.printStackTrace();
}
dialog.dismiss();
}
break;
case DialogInterface.BUTTON_NEGATIVE:
dialog.dismiss();
break;
default:
break;
}
}
};
设置普通按钮不可点击:Button.setEnabled(false);//设置这个属性
java代码中设置或更改一个控件的背景图片:
Resources resources = getContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.layout_bg);
layout.setBackgroundDrawable(btnDrawable);
Android AlertDialog去除黑边白边自定义布局(转)
LayoutInflater inflater = this.getLayoutInflater(); View view = inflater.inflate(R.layout.test_alertdialog_listview,null); ListView lv = (ListView)view.findViewById(R.id.test_alertdialog_listview); lv.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,datas)); lv.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { alertdialog.dismiss(); Toast.makeText(TestAndroid.this, datas[position], Toast.LENGTH_LONG).show(); } }); alertdialog = new AlertDialog.Builder(this) // .setView(view); .setItems(datas, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .create(); // 在此使用setview方法可以设置布局文件和alertdialog四周边框的距离,可以消除黑边框 alertdialog.setView(view, 0, 0, 0, 0); alertdialog.show();