是否有可能在EditText
中使用不同的背景時,它是空的還是空的?不同的背景爲空和填充EditText
回答
你也可以繼承的EditText確實
public class MyEditText extends EditText {
Drawable backgroundEmpty, backgroundFilled;
public MyEditText(Context context) {
super(context);
init(null);
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
private void init(AttributeSet attrs){
if(attrs != null){
backgroundEmpty = ...
backgroundFilled = ...
}else{
backgroundEmpty = ...
backgroundFilled = ...
}
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
setBackground(text.toString().length() == 0 ? backgroundEmpty : backgroundFilled);
}
}
或使用TextWatcher
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
editText.setBackground(s.toString().length() == 0 ? backgroundEmpty : backgroundFilled);
}
});
您可以創建一個Text Watcher並將其添加到EditText。 更改父母佈局(backgroundColor屬性)內並不難。
祝你好運。 (下一次嘗試是你的問題:)更多解釋)
我想補充:如果您打算爲了獲得其中的幾個,可能需要子類化EditText並給這個子類一個輔助背景屬性,所以這可以通過佈局xml來處理。 – Haem
這隻有在您開始使用2-Way Databind Android時才能實現。 這是XML佈局接受條件的唯一方式。 請參閱:https://developer.android.com/topic/libraries/data-binding/index.html#expression_language –
我正在考慮實現子類代碼中的條件,並添加一個名爲,比如在「創建視圖類」(https://developer.android.com/training/custom-views/create-view.html)教程中說的「secondarybackground」。 – Haem
- 1. 的EditText背景圖像填充母
- 2. 填充,列表和背景
- 3. UIImage:填充背景
- 4. CSS - 表格行背景色填充全部填充空間
- 5. 在CSS填充背景/文字填充?
- 6. Django的Imagekit背景填充
- 7. 懸停和填充的背景顏色
- 8. 背景之間不需要的填充背景漸變
- 9. CSS:背景圖像和填充
- 10. 跨度背景色和填充問題
- 11. Highcharts填充欄背景
- 12. jVectormap漸變背景填充
- 13. Bootstrap:總是填充背景?
- 14. 背景填充問題
- 15. 部分填充SVG背景
- 16. 背景未完全填充
- 17. CSS背景調整填充?
- 18. Bootstrap背景寬度填充
- 19. 填充Imageview背景動畫
- 20. 填充背景圖片
- 21. CSS - 部分背景填充
- 22. 填充窗口背景
- 23. 背景顏色不填充div
- 24. Resposive背景不填充移動屏幕
- 25. div背景顏色不會填充格
- 26. 背景圖像不填充所有表
- 27. Android漸變不填充背景
- 28. Android背景不填充屏幕
- 29. 背景圖片不再填充屏幕
- 30. 用不同大小的文本填充父容器背景
你可以推動你的答案更進一步,並提供一些XML代碼^^。 – AnixPasBesoin
xml代碼爲什麼? – lelloman
用於使用自定義EditText。 – AnixPasBesoin