如何显示"清除按钮"在第一个字符被按下并在文本为&#34时隐藏它;"?

时间:2016-07-28 09:21:13

标签: android android-edittext textwatcher

我需要在编辑文本的右侧显示一个清除按钮,并在文本为""时隐藏它。我怎么能这样做。

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

6 个答案:

答案 0 :(得分:2)

Here是你的答案。

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

答案 1 :(得分:2)

看一下下面的代码,它可以帮到你:

SELECT TOP (100) PERCENT
    QuoteName(OBJECT_SCHEMA_NAME(referencing_id)) + '.' + QuoteName(OBJECT_NAME(referencing_id)) AS [this Object...],
        o.type_desc,
    ISNULL(QuoteName(referenced_server_name) + '.', '')
    + ISNULL(QuoteName(referenced_database_name) + '.', '')
    + ISNULL(QuoteName(referenced_schema_name) + '.', '')
    + QuoteName(referenced_entity_name) AS [... depends ON this missing entity name]
    ,sed.referenced_class_desc
    ,case when o.type_desc in( 'SQL_STORED_PROCEDURE' ,'SQL_SCALAR_FUNCTION' ,'SQL_TRIGGER' ,'VIEW')
          then 'EXEC sys.sp_refreshsqlmodule ''' + QuoteName(OBJECT_SCHEMA_NAME(referencing_id)) + '.' + QuoteName(OBJECT_NAME(referencing_id)) + ''';'
          else null
       end as [Refresh SQL Module command]
FROM sys.sql_expression_dependencies as sed
LEFT JOIN sys.objects o
            ON sed.referencing_id=o.object_id
WHERE (is_ambiguous = 0)
    AND (OBJECT_ID(ISNULL(QuoteName(referenced_server_name) + '.', '')
    + ISNULL(QuoteName(referenced_database_name) + '.', '')
    + ISNULL(QuoteName(referenced_schema_name) + '.', '')
    + QuoteName(referenced_entity_name)) IS NULL)
    AND NOT EXISTS
       (SELECT * 
        FROM sys.types 
        WHERE types.name = referenced_entity_name 
        AND types.schema_id = ISNULL(SCHEMA_ID(referenced_schema_name), SCHEMA_ID('dbo'))
       )
GROUP BY ISNULL(QuoteName(referenced_server_name) + '.', '') + ISNULL(QuoteName(referenced_database_name) + '.', '') + ISNULL(QuoteName(referenced_schema_name) + '.', '') + QuoteName(referenced_entity_name)
ORDER BY [this Object...],
[... depends ON this missing entity name]
go

答案 2 :(得分:2)

文字观察者可能会为你工作

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)
  }
}

答案 3 :(得分:1)

从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)
  }
}

答案 4 :(得分:1)

试用此代码

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
    }
     });

答案 5 :(得分:1)

您可以使用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