項目中要使用到帶圖標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