(轉(zhuǎn))EditText hint帶圖片的提示

項目中要使用到帶圖標hint的EditTexit,網(wǎng)上搜到該篇文章,特轉(zhuǎn)來保存以供學(xué)習(xí)回顧。


先上效果:

(忽略掉那個 FloatingActionButton …….路人亂入….)

同學(xué)問的一個問題,編輯子網(wǎng)掩碼的時候不顯示筆,否則顯示.

他都要重寫一個view了

….其實很簡單..

然后他說:當?shù)玫浇裹c時 這個手機圖片不會消失 這樣寫的話

然后改一下:就滿足需求了:

finalEditText mEditText = (EditText) findViewById(R.id.ed);finalDrawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);// 這一步必須要做,否則不會顯示.drawable.setBounds(0,0, drawable.getMinimumWidth(), drawable.getMinimumHeight());

mEditText.addTextChangedListener(newTextWatcher() {@OverridepublicvoidbeforeTextChanged(CharSequence s,intstart,intcount,intafter) {

}@OverridepublicvoidonTextChanged(CharSequence s,intstart,intbefore,intcount) {if(count !=0) {

mEditText.setCompoundDrawables(null,null,null,null);

}

}@OverridepublicvoidafterTextChanged(Editable s) {if(TextUtils.isEmpty(s)) {

mEditText.setCompoundDrawables(drawable,null,null,null);

}

}

});

========================================

關(guān)鍵答案來自:使用代碼為textview設(shè)置drawableLeft

正文:

我想在代碼中改變drawable。

有什么方法可以使用代碼為textview設(shè)置drawableLeft呢?

解決方案:

publicvoidsetCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom);

1

類似調(diào)用方法如下:

1.在XML中使用

android:drawableLeft=”@drawable/icon”

2.代碼中動態(tài)變化

Drawable drawable= getResources().getDrawable(R.drawable.drawable);/// 這一步必須要做,否則不會顯示.drawable.setBounds(0,0, drawable.getMinimumWidth(), drawable.getMinimumHeight());myTextview.setCompoundDrawables(drawable,null,null,null);


也或參考另一個函數(shù)

public void setCompoundDrawablesWithIntrinsicBounds (Drawableleft,Drawabletop, Drawableright, Drawablebottom)

版權(quán)聲明:http://blog.csdn.net/u013768203

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容