【问题标题】:How to show a "Clear button" after the first character pressed and hide it when the text is ""?如何在按下第一个字符后显示“清除按钮”并在文本为“”时隐藏它?
【发布时间】:2016-12-02 14:19:12
【问题描述】:

我需要在编辑文本的右侧显示一个清除按钮,并在文本为“”时将其隐藏。我该怎么做。

我只需要知道文本长度>0或=0时如何显示和隐藏,仅此而已。

【问题讨论】:

标签: android android-edittext textwatcher


【解决方案1】:

Here 是你的答案。

使用代码并通过检查字符长度在 textchange 上打开和关闭按钮可见性。

【讨论】:

    【解决方案2】:

    看看下面的代码,它可能对你有帮助:

    EditText etSearch = (EditText) view.findViewById(R.id.etSearch);
    
    etSearch.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    String str = etSearch.getText().toString();
                    if (str.isEmpty()) {
                        btnClear.setVisibility(View.INVISIBLE);
                    } else {
                        btnClear.setVisibility(View.VISIBLE);
                    }
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });
    

    【讨论】:

      【解决方案3】:

      文本观察器可能对你有用

      If(editText1.getText.toString.length>0)
       {
        if(button1.getVisibility == View.GONE)
        {
        button1.setVisibility(View.VISIBLE)
        }
      }
      else
      {
       if(button1.getVisibility == View.VISIBLE)
        {
        button1.setVisibility(View.GONE)
        }
      }
      

      【讨论】:

        【解决方案4】:

        试试这个代码

        Field1.addTextChangedListener(new TextWatcher() {
        
             @Override
              public void afterTextChanged(Editable s) {}
        
             @Override    
               public void beforeTextChanged(CharSequence s, int start,
               int count, int after) {
              }
        
            @Override    
            public void onTextChanged(CharSequence s, int start,
               int before, int count) {
               if(s.length() != 0)
                // set the visibility
            }
             });
        

        【讨论】:

        • @andri : 没搞定这个
        【解决方案5】:

        您可以使用TextWatcher,一种侦听器:

        yourEditText.addTextChangedListener(new TextWatcher() {
        
              public void afterTextChanged(Editable s) {
        
                //Here, you can check for text size...
                int length = editText.toString().length();
                 [...]
        
        
        
              }
        
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        
              public void onTextChanged(CharSequence s, int start, int before, int count) {}
           });
        }
        
        }
        

        取自this question

        【讨论】:

          【解决方案6】:

          从 xml 设置它的可见性 GONE。

          If(editText1.getText.toString.length>0)
           {
            if(button1.getVisibility == View.GONE)
            {
            button1.setVisibility(View.VISIBLE)
             }
          }
          else
          {
           if(button1.getVisibility == View.VISIBLE)
            {
            button1.setVisibility(View.GONE)
            }
          }
          

          【讨论】:

          • 为什么消失而不是隐形?
          猜你喜欢
          • 2016-04-14
          • 1970-01-01
          • 1970-01-01
          • 2017-08-06
          • 1970-01-01
          • 1970-01-01
          • 2022-08-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多