dialog中的button动态设置为disable[转]
我们再写dialog的时候,会时常有这样一种需求,希望通过某些条件将dialog的button设置为disable的。
基本的命令就是将“确定”这个button设置为disable(false).
如下的方法,就是构造一个自定义的dialog,其中包括一个编辑栏(EditText)和两个按钮(确定和取消)
如果想要当EditText为空的时候让确定按钮为不可点击状态 你可能会如下实现(但是这个里面有问题!!!)。

public Dialog customDialog(Context dialogContext){
final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
builder.setView(editText); //将一个EditText放入dialog
builder.setTitle(R.string.fastdialer_add_number_title); //设置dialog的Title
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//点击确定后干点什么......
}
});
//希望拿到“确定”按钮。初始化确定按钮
final Button positiveButton = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE);
if(edittext.getText().toString.equal("")) //初次进来为空的时候,就设置按钮为不可点击
positiveButton.setEnabled(false);
editText.addTextChangedListener(//设置编辑栏的文字输入监听
new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
if(arg0.toString().equals("")){ //当编辑栏为空的时候,将按钮设置为不可点击。
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(true);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
}
);
return dialog; //返回dialog 由外层去dialog.show();
}

以上的这段代码编译是可以通过的,但是运行的时候,你会得到一个空指针异常。你猜的出来是哪个对象为空了么?
坑爹阿 就是
final Button positiveButton = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE);
另外如果想要判断editText的字符串是不是为空,应该editText.getText().toString().equal("");一定要toString().
否则这个判断会有问题。
所以请记住结论如下:
如果你想得到一个alertDialog中的button你必须在这个dialog.show()之后才能够拿到。
所以以上的代码可以更改为

private void createCustomDialog(Context dialogContext, final int position, String defaultNumber) {
final EditText editText = new EditText(dialogContext);
final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
builder.setView(editText);
builder.setTitle(R.string.fastdialer_add_number_title);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//任何你想做的事情
});
builder.setNegativeButton(android.R.string.cancel, null);
Dialog dialog = builder.create();
dialog.show();//!!!!!!!!!!!!!!看这里,先把dialog show出来。
final Button positiveButton = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE);
//现在这个dialog的button不会再是空的了!!!!!!!!!!!
if(editText!=null && editText.getText().toString().equals(""))
positiveButton.setEnabled(false);
customType.addTextChangedListener(
new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
if(arg0.toString().equals("")){
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(true);
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
}
);
}

以上代码是不是完美了呢?
其实dialog里面还有一个监听方法,可以当dialog show出来之后回调
所以你可以把要在show()之后做的事情放在这个监听方法里面,如下:

final Dialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener(){//开始监听 show
private Button positiveButton;
@Override
public void onShow(DialogInterface arg0) {
if(positiveButton == null)//先初始化相关的按钮
positiveButton = ((AlertDialog)arg0).getButton(AlertDialog.BUTTON_POSITIVE);
if(customType != null&&customType.getText().toString().equals("")){//判断编辑栏,记得要toString().
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(true);
}
}});
customType.addTextChangedListener(new TextWatcher() {
private Button positiveButton;
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {} public void afterTextChanged(Editable arg0) {
if(positiveButton == null) {//此时dialog已经show出来了 所以应该也可以拿到按钮对象。
positiveButton = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE);
}
String content = arg0.toString();
if(content == null || content.equals("")) {
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(true);
}
}
});

如果想让某一个button 高亮显示

dlg.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
// positiveButton.setFocusable(true); 之所以注释掉是因为没用
positiveButton.setFocusableInTouchMode(true); // 这个属性置为true后才有用
positiveButton.requestFocus();
}
});
dialog中的button动态设置为disable[转]的更多相关文章
- 利用StateListDrawable给button动态设置背景
项目中,遇到相同样式的Button,只是stroke颜色不一样.为了实现一个,就得写两个shape文件,一个selector文件:多个还得重复写. 解决方法: 结合StateListDrawable给 ...
- 【原创】如何在Android中为TextView动态设置drawableLeft等
如何在Android中为TextView动态设置drawableLeft等 两种方式: 方式1:手动设置固有边界 Drawable drawable = getResources().getD ...
- iOS-UITextField中给placeholder动态设置颜色的四种方法
思路分析: 0.自定义UITextField 1.设置占位文字的颜色找-->placeholderColor,结果发现UITextField没有提供这个属性 2.在storyboard/xib中 ...
- DataSet中的表动态设置主键外键的方法
原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] protected void pk_Click(object sender, EventArgs e) { ...
- vue中动态设置echarts画布大小
document.getElementById('news-shopPagechart').style.height = this.heightpx2+'px'; //heightpx2定义在data ...
- 【Android】TextView动态设置android:drawableLeft|Right|Top|Bottom,SetColor
Android中有时需动态设置控件四周的drawble图片,这个时候就需要调用 setCompoundDrawables(left, top, right, bottom),四个参数类型都是drawa ...
- Android Button 按钮 设置 各种状态 图片 颜色
有2个方法可以实现,一种是用 选择器 定义每种状态的图片 selec.xml <?xml version="1.0" encoding="utf-8"?& ...
- siverlight 后台动态设置图片路径的总结
最近碰到了个问题,需要给一个用户控件中的image动态设置图片资源 1.图片资源属性为resource时,静态引用无任何问题,但是动态设置时,就什么也不显示 后来找到问题所在, 必须把此图片属性项中“ ...
- Ext.form.Label组件动态设置html值
解决方法: (1)用的是 Ext.getCmp(id).setText('XXXX')可以动态设置label 显示的文本值,但是文本中有个别数字需要改变颜色显示,需要加样式,这种方法会把加样式的标签( ...
随机推荐
- Oracle 11g详细安装配置教程
最近开始学习Oracle数据库了,根据我的理解Oracle数据库是一种强大.复杂.高性能的数据库,而且价格不菲,使用都是中大型企业,土豪专用.学习一种数据库的入门工作就是先安装这种数据库,鉴于大批道友 ...
- 函数学习(JY07-JavaScript-JS基础03)
- 打jar包的方法
打jar包的方法是什么? java打jar包,引用其他.jar文件 java项目打jar包 将java源码打成jar包 maven打jar例子 打war包的方法是什么? Eclipse->项目右 ...
- Session深度探索
什么是Session? web是无状态,这意味着每次页面被回传到服务器时,都重新生成一个web页面类的一个新的实例.众所周知http时无状态的协议.它不能获得客户端的信息.如果用户录入了一些信息,当跳 ...
- 历史执行Sql语句性能分析 CPU资源占用时间分析
SELECT HIGHEST_CPU_QUERIES.PLAN_HANDLE, HIGHEST_CPU_QUERIES.TOTAL_WORKER_TIME, Q.DBID, ...
- 神器-Sublime Text 3 代码编辑器安装与使用
一.软件获取 1.软件下载地址:http://www.sublimetext.com/3. 2.注册机和汉化下载:http://files.cnblogs.com/files/1312mn/subli ...
- 重复造轮子感悟 – XLinq性能提升心得
曾经的两座大山 1.EF 刚接触linq那段时间,感觉这家伙好神奇,语法好优美,好厉害.后来经历了EF一些不如意的地方,就想去弥补,既然想弥补,就必须去了解原理.最开始甚至很长一段时间都搞不懂IQue ...
- 详解CSS选择器、优先级与匹配原理
原文链接:http://polaris1119.javaeye.com/blog/764428 作为一个Web开发者,掌握必要的前台技术也是很重要的,特别是在遇到一些实际问题的时候.这里给大家列举一个 ...
- xdebug使用说明
常用配置 xdebug.var_display_max_children整数类型,默认值128.用于控制通过xdebug_var_dump(),var_dump()方法时显示数组中子数组的个数或对象中 ...
- iphone微信长按二维码识别不了
在安卓版的微信长按二维码可以识别(前提是你的微信版本到支持此功能),但是到了苹果版的微信就识别不了,经个人测试发现是缩放的问题: 1.设置了初始缩放设置为1,最大缩放值要>=1,不支持缩放.-- ...