简体   繁体   English

EditText,如何隐藏DrawableRight

[英]EditText, how to hide DrawableRight

I want to have DrawableRight in my text area, but when i start typing then it should disappear. 我想在文本区域中包含DrawableRight,但是当我开始键入内容时,它应该会消失。 So i have code for EditText and function that knows when to hide drawable, but i don't know how to call that function. 所以我有EditText和函数的代码,这些代码知道何时隐藏可绘制对象,但我不知道如何调用该函数。 Can you help me? 你能帮助我吗?

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

SOLUTION: I handled with it! 解决方案:我处理了! First of all i changed "this" to "MainActivity.this". 首先,我将“ this”更改为“ MainActivity.this”。 Second was very important - set bounds for drawable! 第二点非常重要-设定可绘制范围! Done. 完成。

You should implement TextWatcher to your Edittext. 您应该实现TextWatcher您EDITTEXT。
Pls. PLS。 see an example here . 在这里看到一个例子。

In onTextChanged you can check - if CharSequence s length is > 0, then you have some text entered and should hide drawable. onTextChanged您可以检查-如果CharSequence s长度> 0,那么您输入了一些文本并且应该隐藏可绘制的。
CharSequence s is a param that you receive in onTextChanged method. CharSequence s是您在onTextChanged方法中收到的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM