【问题标题】:EditText, how to hide DrawableRightEditText,如何隐藏 DrawableRight
【发布时间】:2017-05-16 18:49:43
【问题描述】:

我想在我的文本区域中有 DrawableRight,但是当我开始输入时它应该会消失。所以我有 EditText 的代码和知道何时隐藏 drawable 的函数,但我不知道如何调用该函数。你能帮帮我吗?

//XML CODE
<EditText
        android:id="@+id/textt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:ems="10"
        android:paddingLeft="2dp"
        android:hint="Wpisz wiadomość"
        android:background="@android:color/transparent"
        android:maxLines="4" />

//JAVA CODE
public void camera(View v)
{
    EditText textArea=(EditText) findViewById(R.id.textt);
    if(textArea.getText()==null)
    {
        textArea.setCompoundDrawables(null, null, ContextCompat.getDrawable(this,R.drawable.ic_camera_alt_black_18dp), null);
    }
    else
        textArea.setCompoundDrawables(null,null,null,null);
}

解决方案: 我处理了它!首先,我将“this”更改为“MainActivity.this”。第二个非常重要 - 为可绘制设置边界!完成。

【问题讨论】:

    标签: android android-edittext android-drawable


    【解决方案1】:

    您应该在您的 Edittext 中实现 TextWatcher
    请。看一个例子here

    onTextChanged 中,您可以检查 - 如果 CharSequence s 长度 > 0,那么您输入了一些文本并且应该隐藏 drawable。
    CharSequence s 是您在 onTextChanged 方法中收到的参数。

    【讨论】:

    • 快完成了。现在我在这段代码中遇到参数“this”的问题 textArea.setCompoundDrawables(null, null, ContextCompat.getDrawable(this,R.drawable.ic_camera_alt_black_18dp), null); : 错误的第一个参数类型。找到:'android.text.TextWatcher',需要:'android.content.Context'
    • 如果此代码在您的活动中(例如MainActivity),请放置MainActivity.this 而不是this。如果它在片段内,请输入getActivity()。发生这种情况是因为您的代码现在在 TextWatcher 内,所以 this 现在指向 TextWatcher
    【解决方案2】:

    为了隐藏 textArea 的 drabwable,你可以试试这个:

    Drawable[] drawables = textArea.getCompoundDrawables();
    for (Drawable d: drawables) {
        if (d != null) {
            d.setAlpha(0);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 2011-05-24
      • 2013-07-12
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2014-08-07
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多