gpt4 book ai didi

android - 为什么 alert.getButton(AlertDialog.BUTTON_POSITIVE) 返回 NULL?

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:31 26 4
gpt4 key购买 nike

(不好意思,发现这个问题已经有人回答了,见下文)

所以,我先tried to extend the AlertDialog这样我就可以用弹出窗口中需要的 EditText 做一些事情,但是当 I couldnt get that to work ,我现在在我的 MainActivity 中使用普通的 AlertDialog.Builder 来创建我需要的东西。

下面的代码创建了一个 AlertDialog 并使用“setView”将我的自定义 View 添加到其中。当 AlertDialog 第一次显示时,它的 POSITIVT 按钮应该被禁用,只有当所有的 EditTexts 都有文本时,POSITIVT 按钮才应该被启用。

所以我尝试通过在使用 builder.create() 之后获取肯定按钮来实现这一点。

但是,alert.getButton(AlertDialog.BUTTON_POSITIVE) 返回 NULL。为什么?

final EditText ed1 = (EditText) findViewById(R.id.editTextPausArea);
final EditText ed2 = (EditText) findViewById(R.id.EditTextPausTimeFrom);
final EditText ed3 = (EditText) findViewById(R.id.EditTextPausTimeTo);

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("Begär paus")
.setView(view)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {


}
}).setNegativeButton("Avbryt", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
});

final AlertDialog alert = builder.create();
final Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);
b.setEnabled(false); // NullPointerException here!

最佳答案

我遇到了同样的问题。我对这个问题的工作解决方案是调用:

alert.show()

在访问按钮之前

关于android - 为什么 alert.getButton(AlertDialog.BUTTON_POSITIVE) 返回 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7834680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com