2017-05-16 34 views
0

我想在我的文本區域有DrawableRight,但是當我開始輸入時,它應該會消失。所以我有編輯EditText和函數,知道什麼時候隱藏drawable,但我不知道如何調用該函數。你可以幫我嗎?EditText,如何隱藏DrawableRight

//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: 我用它來處理!首先我將「this」改爲「MainActivity.this」。其次是非常重要的 - 設置可繪製的邊界!完成。

回答

0

您應該對您的Edittext實施TextWatcher
請注意。請參閱示例here

onTextChanged你可以檢查 - 如果CharSequence s長度> 0,那麼你有一些文本輸入,應該隱藏drawable。
CharSequence s是您在onTextChanged方法中收到的參數。

+0

幾乎完成。現在我有這個代碼中的參數「this」的問題textArea.setCompoundDrawables(null,null,ContextCompat.getDrawable(this,R.drawable.ic_camera_alt_black_18dp),null); :錯誤的第一個參數類型。找到:'android.text.TextWatcher',必需:'android.content.Context' –

+0

如果這個代碼在你的活動中(例如'MainActivity'),放置'MainActivity.this'而不是'this'。如果它在一個片段中,則使用'getActivity()'。發生這種情況的原因是您的代碼現在位於'TextWatcher'內,因此'this'現在指向'TextWatcher'。 –