如何将“提交”选项灰显当特定的 EditText 框为空时按钮?

发布于 2024-11-04 22:53:32 字数 253 浏览 1 评论 0原文

关于我正在创建的 Android 应用程序:

我有三个 EditText 框需要填充数字/字符串。我有一个提交按钮,它将开始一系列计算。

如果任何框为空并按下提交,应用程序就会崩溃。我尝试用 try-catch 语句来做到这一点,但没有成功。我只是想禁用该按钮,直到三个框都有数字。我知道有一种方法可以 setEnabled(false) 我想?或者有更好的方法吗?这会使按钮变灰吗?或者这是一个与 setEnabled 无关的函数?

Regarding an Android app I am creating:

I have three EditText boxes that need to be filled with numbers/strings. I have a submit button that will start a series of calculations.

IF any box is empty and submit is pressed, the app crashes. I have tried to do this with try-catch statement, but it is not working out. I simply want to disable the button until three boxes have numbers. I know there is a way to setEnabled(false) I think? Or is there a better way? Will this grey out the button? Or is that an unrelated function to setEnabled?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桜花祭 2024-11-11 22:53:32

尝试这个解决方案。

EditText edit1;
EditText edit2;
EditText edit3;
View button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Your initialization code...

    TextWatcher watcher = new LocalTextWatcher();
    edit1.addTextChangedListener(watcher);
    edit2.addTextChangedListener(watcher);
    edit3.addTextChangedListener(watcher);
    updateButtonState();
}

void updateButtonState() {
    boolean enabled = checkEditText(edit1)
        && checkEditText(edit2)
        && checkEditText(edit3);
    button.setEnabled(enabled);
}

private boolean checkEditText(EditText edit) {
    return Integer.getInteger(edit.getText().toString()) != null;
}

private class LocalTextWatcher implements TextWatcher {
    public void afterTextChanged(Editable s) {
        updateButtonState();
    }

    void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    void onTextChanged(CharSequence s, int start, int before, int count) {
    }
}

Try this solution.

EditText edit1;
EditText edit2;
EditText edit3;
View button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Your initialization code...

    TextWatcher watcher = new LocalTextWatcher();
    edit1.addTextChangedListener(watcher);
    edit2.addTextChangedListener(watcher);
    edit3.addTextChangedListener(watcher);
    updateButtonState();
}

void updateButtonState() {
    boolean enabled = checkEditText(edit1)
        && checkEditText(edit2)
        && checkEditText(edit3);
    button.setEnabled(enabled);
}

private boolean checkEditText(EditText edit) {
    return Integer.getInteger(edit.getText().toString()) != null;
}

private class LocalTextWatcher implements TextWatcher {
    public void afterTextChanged(Editable s) {
        updateButtonState();
    }

    void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    void onTextChanged(CharSequence s, int start, int before, int count) {
    }
}
未央 2024-11-11 22:53:32

您应该在 XML 中默认禁用该按钮,以便用户不会意外点击该按钮。之后,您需要查看您的字段并确保所有字段在提交之前都有数据。

您可以这样做,或者您可以提交对所有字段进行快速检查并确保它们都不等于“”。

基本上看起来像这样(如果你想忽略隐藏按钮并在检查后处理处理)

if (!((t1.getText().toString.compareTo("") == 0) && (t2.getText().toString.compareTo("")==0) ...))
{
Do stuff
}

else
{
Toast message here
}

否则你可以像上面提到的海报那样有一个“观察者”。

You should disable the button by default in the XML so that the user cannot hit the button by accident. After this you need to look at your fields and insure all of them have data before the submission.

You could do this, or you could just have submit run a quick check on all the fields and insure none of them are equal to "".

Basically look like this (If you want to ignore hiding the button and just handle processing after the check)

if (!((t1.getText().toString.compareTo("") == 0) && (t2.getText().toString.compareTo("")==0) ...))
{
Do stuff
}

else
{
Toast message here
}

Otherwise you can just have a "watcher" like the above poster mentioned.

眼眸印温柔 2024-11-11 22:53:32

您也可以使用此支票

boolean checkEditText(EditText editText) {
    return editText.getText().toString().trim().equals("");
}

You can also use this check

boolean checkEditText(EditText editText) {
    return editText.getText().toString().trim().equals("");
}
迟月 2024-11-11 22:53:32

您好,我已经尝试了上面的代码,并将函数更改为下面给出的,以使其正常工作。

private boolean checkEditText(EditText edit) {
    return ((edit.getText().toString()).length() >0 );
}

Hi i have tried above code and changed the function to given below for it to work.

private boolean checkEditText(EditText edit) {
    return ((edit.getText().toString()).length() >0 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文