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 显示的文本值,但是文本中有个别数字需要改变颜色显示,需要加样式,这种方法会把加样式的标签( ...
随机推荐
- webform 复杂点的服务器控件
1 , dropdownlist: 下拉框 属性items 列表集合, 里面的每一个元素是一个 listitem . 联动的时候注意要 设置属性 .Autopostback 为ture: 注注 ...
- 《CSS网站布局实录》学习笔记(三)
第三章 CSS网页布局与定位 3.1 div 几乎XHTML中的任何标签都可以用于浮动与定位,而div首当其冲.对于其他标签而言,往往有它自身存在的目的,而div元素存在的目的就是为了浮动与定位. 3 ...
- 关于oracle dblink的知识。
create database link WZGLconnect to MMCSG_GX(用户名)using '(description=(address_list=(address=(host=xx ...
- Swift - 40 - 枚举更加灵活的使用方式
//: Playground - noun: a place where people can play import UIKit /* 这里的枚举没有给它的成员默认值, 而是给它绑定了一个类型, 之 ...
- JNDI--Java命名和目录接口
JNDI主要用于在容器中配置某些资源,让所有项目可以使用.JNDI可以提供: 1:数据库连接池. 自定义连接池 第三方连接池 Dbcp ...
- geotools导入shp文件到Oracle数据库时表名带下划线的问题解决
问题: 最近在做利用geotools导入shp文件到Oracle表中,发现一个问题Oracle表名带下划线时导入失败,问题代码行: dsOracle.getFeatureWriterAppend(or ...
- linux 目录说明
1./bin /usr/bin /usr/local/bin 都是放置用户可执行二进制文件. 2./boot 主要是放置liunx系统启动时用到的文件. 2./dev 文件夹内主要是西东外设 ...
- HTML滚动条
水平没有滚动条 <body scroll="no" style="overflow-x:hidden"> 垂直没有滚动条 <body scro ...
- NUMBER BASE CONVERSION(进制转换)
Description Write a program to convert numbers in one base to numbers in a second base. There are 62 ...
- 个人vim配置(.vimrc文件分享)
syntax enable syntax on colorscheme desert set nu! set nowrap set nobackup set backspace= set tabsto ...