dialog中的button动态设置为disable[转]
2024-09-26 12:56:17
我们再写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();
}
});
最新文章
- SharePoint 2010 GridView/SPGridView完全应用系统样式
- JavaScript的个人学习随手记(一)
- ACM 阶乘之和
- android压力测试命令monkey详解
- 【规范】javascript 变量命名规则
- define的用法
- ios tweak之binary not signed (use ldid -S)问题解决
- 转--DataTable 修改列名 删除列 调整列顺序
- Android自定义控件 开源组件SlidingMenu的项目集成
- 双击td字段,出现编辑文本框(更改之后发送数据请求) jsp
- [转] MMO即时战斗:地图角色同步管理和防作弊实现
- Swift 入门之简单语法(二)
- Win10 之最新最简单有效安装配置adb
- 01_Weblogic课程之概念篇:代理服务器,web服务器,应用程序服务器,JNDI概念,JTA概念,Java消息服务,Java验证和授权(JAAS),Java管理扩展,Web客户机,客户机应用程序
- JLOI2018 划水中...
- 使用font awesome制作网站常用社交工具联系方式图标
- 页面中关于bootstrap框架的增删改查使用
- CentOS 6.4 安装 rabbitmq(3.6.15)
- jQuery实现一个淡入淡出下拉菜单 非常简易
- 怎样在windows上定时执行python脚本