• <bdo id='Qlmgb'></bdo><ul id='Qlmgb'></ul>

      <small id='Qlmgb'></small><noframes id='Qlmgb'>

    1. <tfoot id='Qlmgb'></tfoot>
        <i id='Qlmgb'><tr id='Qlmgb'><dt id='Qlmgb'><q id='Qlmgb'><span id='Qlmgb'><b id='Qlmgb'><form id='Qlmgb'><ins id='Qlmgb'></ins><ul id='Qlmgb'></ul><sub id='Qlmgb'></sub></form><legend id='Qlmgb'></legend><bdo id='Qlmgb'><pre id='Qlmgb'><center id='Qlmgb'></center></pre></bdo></b><th id='Qlmgb'></th></span></q></dt></tr></i><div id='Qlmgb'><tfoot id='Qlmgb'></tfoot><dl id='Qlmgb'><fieldset id='Qlmgb'></fieldset></dl></div>
      1. <legend id='Qlmgb'><style id='Qlmgb'><dir id='Qlmgb'><q id='Qlmgb'></q></dir></style></legend>

        Android - 在用户输入之前和之后检查editText是否> 3

        时间:2023-05-28

          <small id='s3giR'></small><noframes id='s3giR'>

          <legend id='s3giR'><style id='s3giR'><dir id='s3giR'><q id='s3giR'></q></dir></style></legend>

              <tbody id='s3giR'></tbody>
              <i id='s3giR'><tr id='s3giR'><dt id='s3giR'><q id='s3giR'><span id='s3giR'><b id='s3giR'><form id='s3giR'><ins id='s3giR'></ins><ul id='s3giR'></ul><sub id='s3giR'></sub></form><legend id='s3giR'></legend><bdo id='s3giR'><pre id='s3giR'><center id='s3giR'></center></pre></bdo></b><th id='s3giR'></th></span></q></dt></tr></i><div id='s3giR'><tfoot id='s3giR'></tfoot><dl id='s3giR'><fieldset id='s3giR'></fieldset></dl></div>
              • <bdo id='s3giR'></bdo><ul id='s3giR'></ul>
                <tfoot id='s3giR'></tfoot>

                  本文介绍了Android - 在用户输入之前和之后检查editText是否> 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些代码,如果 editText 字段中的字符少于 3 个,我需要禁用创建帐户"按钮.如果用户输入 3 个字符,则该按钮应启用自身以便可以使用.

                  I have some code where I need a "Create Account" button to be disabled if an editText field has less than 3 char in it. If the User enters 3 chars, then the button should enable itself so that it can be used.

                  我已经构建了 if else 语句,如果 editText 字段中的字符少于 3 个,则禁用按钮,但是在输入时,当用户插入 3 个字符时,它不会重新评估该语句是否为真所以按钮当然会保持禁用状态.

                  I have constructed the if else statement that disables the button if there are less than 3 char in the editText field, BUT on input, when the user inserts 3 char, it does not re-evaluate to see if the statement is true so the button of course stays disabled.

                  一旦用户在编辑文本字段中输入 3 个字符,按钮应该会自行启用.

                  Once the user enters 3 char into the edit text field, the button should enable itself.

                      Button buttonGenerate = (Button) findViewById(R.id.btnOpenAccountCreate);
                      userInitials = (EditText) findViewById(R.id.etUserChar);
                      if (userInitials.getText().toString().length() > 3) {
                                  // Account Generator Button
                                              buttonGenerate.setEnabled(true); // enable button;
                                              buttonGenerate.setOnClickListener(new OnClickListener() { 
                  //Do cool stuff here
                                                  @Override
                                                  public void onClick(View v) {
                  
                                                  }
                                              });// END BUTTON
                              } else {
                  
                                  // If UserInitials is empty, disable button
                                              Toast.makeText(this, "Please enter three(3) characters in the Initials Field ", Toast.LENGTH_LONG)
                                                      .show();
                                              buttonGenerate.setEnabled(false); // disable button;
                              }// END IF ELSE
                  

                  推荐答案

                  你想使用一个 TextWatcher

                  每当您的 EditText 中具有此 listener 的文本发生更改时,都会触发此事件.您只需像其他任何 listener 一样将 listener 附加到您的 EditText 然后覆盖其方法,并从下面的链接示例中检查长度

                  This will be triggered each time the text in your EditText which has this listener on it has changed. You just attach the listener to your EditText like you would any other listener then override its methods and , from the linked example below, check the length

                   @Override
                  public void onTextChanged(CharSequence s, int start, int before, int count) 
                  {   
                       if (s.length() > 2) 
                       {
                           buttonGenerate.setEnabled(true);
                       }
                       else
                      {
                           buttonGenerate.setEnabled(true);
                      }
                  }
                  

                  您不需要签入您的 onClick() 然后,只需默认禁用 Button 并在您的 onTextChanged() 如果满足条件.

                  You don't need to check in your onClick() then, just disable the Button by default and enable in your onTextChanged() if the condition is met.

                  重写

                  上面可以清理为

                  @Override
                  public void onTextChanged(CharSequence s, int start, int before, int count) {   
                       buttonGenerate.setEnabled((s.length() > 2));
                  }
                  

                  我也改成了>2 因为我认为这实际上是你想要的,但你拥有它的方式有点令人困惑.您说输入三(3)",这听起来正好是 3,但您的代码看起来不同.无论如何,这对你来说很容易改变.

                  I also have changed it to > 2 because I think that's actually what you want but the way you have it is a little confusing. You say "enter three(3)" which sounds like exactly 3 but your code looks different. Anyway, that's easy enough for you to change.

                  查看这个答案的例子

                  这篇关于Android - 在用户输入之前和之后检查editText是否> 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android 警报对话框无法找到视图 下一篇:如何使用 Google UiAutomator 按两次按钮?

                  相关文章

                    <tfoot id='bYqaT'></tfoot>

                    • <bdo id='bYqaT'></bdo><ul id='bYqaT'></ul>
                  1. <legend id='bYqaT'><style id='bYqaT'><dir id='bYqaT'><q id='bYqaT'></q></dir></style></legend>
                    <i id='bYqaT'><tr id='bYqaT'><dt id='bYqaT'><q id='bYqaT'><span id='bYqaT'><b id='bYqaT'><form id='bYqaT'><ins id='bYqaT'></ins><ul id='bYqaT'></ul><sub id='bYqaT'></sub></form><legend id='bYqaT'></legend><bdo id='bYqaT'><pre id='bYqaT'><center id='bYqaT'></center></pre></bdo></b><th id='bYqaT'></th></span></q></dt></tr></i><div id='bYqaT'><tfoot id='bYqaT'></tfoot><dl id='bYqaT'><fieldset id='bYqaT'></fieldset></dl></div>

                    <small id='bYqaT'></small><noframes id='bYqaT'>