AlertDialog自定义关闭

如果AlertDialog里有输入框,那么如何根据自己的需要关闭AlertDialog,网上有很多是通过反射方式。这里有另外一种方法。

Builder builder = new Builder(MainActivity.this);

builder.setTitle("服务器设置");

LayoutInflater inflater = LayoutInflater.from(this);

final View view = inflater.inflate(R.layout.edit, null);

builder.setView(view);

builder.setPositiveButton(R.string.ok, new AlertDialog.OnClickListener(){

@Override

public void onClick(DialogInterface dialog, int which) {

}

});

builder.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener(){

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}

});

final AlertDialog dialog = builder.create();

//builder.show();

dialog.show();

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()

{

@Override

public void onClick(View v)

{

Boolean wantToCloseDialog = false;

//需要时修改wantToCloseDialog的值

if(wantToCloseDialog){

dialog.dismiss();

}else{

Toast.makeText(MainActivity.this, "服务器地址不符合规范", Toast.LENGTH_SHORT).show();

}

}

});

AlertDialog自定义关闭,布布扣,bubuko.com

时间: 2024-08-02 10:59:21

AlertDialog自定义关闭的相关文章

获取 AlertDialog自定义的布局 的控件

AlertDialog自定义的布局 效果图: 创建dialog方法的代码如下: 1 LayoutInflater inflater = getLayoutInflater(); 2 View layout = inflater.inflate(R.layout.dialog, 3 (ViewGroup) findViewById(R.id.dialog)); 4 new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout

AlertDialog自定义View的用法+如何改变弹出框的大小

android系统定义了弹出框,支持我们自定义布局: public AlertDialog getEditCustomDialog() { LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.custom_message_rename, null); AlertDialog.Builder builder = new AlertDialog.Builder(AnimationTe

AlertDialog自定义效果

/** * 自定义AlertDialog * 用于退出程序按钮 * */ public class AlertDialog { Context context; android.app.AlertDialog ad; TextView titleView; TextView messageView; LinearLayout buttonLayout; public AlertDialog(Context context) { this.context = context; ad = new a

Android进阶之AlertDialog自定义

AlertDialog的自定义方式有很多种,这里介绍两种. 第一种是比较简单的,只自定义内容. 在AlertDialog使用详解中,非常详细的介绍了以下六种使用方法. 一.简单的AlertDialog(只显示一段简单的信息,比如about us) 二.带按钮的AlertDialog(显示提示信息,让用户操作,比如exit时的警告框) 三.类似ListView的AlertDialog(展示内容,比如某人的一些注册信息) 四.类似RadioButton的AlertDialog(让用户选择,单选) 五

【转】Android AlertDialog自定义布局

原文网址:https://blog.csdn.net/u010694658/article/details/53022294 由于开发中经常使用弹框,然而系统自带的弹框太局限,也不太美观,经常不能满足开发需求,所以就只能自定义布局.其实自定义布局很简单,没不要写出来,但是如果不写一遍的,后面遇到的话就感觉又会忘记,所以在次记一小笔,仅记一个最简单的例子,可以举一反三.  直接上代码 public class MainActivity extends Activity implements OnC

android alertdialog 自定义button监听事件

AlertDialog Dialog = new AlertDialog.Builder(Huntinfo.this).create(); Dialog.show(); Dialog.getWindow().setGravity(Gravity.BOTTOM); Dialog.getWindow().setLayout( android.view.WindowManager.LayoutParams.FILL_PARENT, android.view.WindowManager.LayoutPa

AlertDialog自定义布局

activity调用 private void showAlertDialog() { final AlertDialogUtil dialog = new AlertDialogUtil(this, false, null); // false代表必须点击"确定"其它不可以点击不消失,true点击其它也消失 dialog.setMessage("您尚未登录"); dialog.setBtnPositiveValue("确定"); dialog.

Android实现点击AlertDialog上按钮时不关闭对话框

开发过程中,有时候会有这样的需求: 点击某个按钮之后显示一个对话框,对话框上面有一个输入框,并且有“确认”和“取消”两个按钮.当用户点击确认按钮时,需要对输入框的内容进行判断.如果内容为空则不关闭对话框,并toast提示. 使用AlertDialog.Builder创建对话框时,可以使用builder.setNegativeButton和builder.setPositiveButton两个方法设置取消按钮和确认按钮的点击事件.然而问题在于,只要用户点击了确认按钮或者取消按钮,系统就会自动将对话

Android:实现点击AlertDialog上按钮时不关闭对话框

开发过程中,有时候会有这样的需求: 点击某个按钮之后显示一个对话框,对话框上面有一个输入框,并且有"确认"和"取消"两个按钮.当用户点击确认按钮时,需要对输入框的内容进行判断.如果内容为空则不关闭对话框,并toast提示. 使用AlertDialog.Builder创建对话框时,可以使用builder.setNegativeButton和builder.setPositiveButton两个方法设置取消按钮和确认按钮的点击事件.然而问题在于,只要用户点击了确认按钮或