7
A
回答
4
創建你自己定製的EditText控制
下面是我爲你做一個例子:
選擇時,你探微必須改變mPaint.setColor(Color.GREEN);另一種顏色
public class CustomEditText extends EditText{
private Rect mRect;
private Paint mPaint;
int widthMsSize;
int heightMsSize ;
// we need this constructor for LayoutInflater
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.GREEN);
System.out.println("constructor");
}
protected void onMeasure(final int widthMeasureSpec,
final int heightMeasureSpec) {
// Extract the Ms (MesaureSpec) parameters
widthMsSize = MeasureSpec.getSize(widthMeasureSpec);
heightMsSize = MeasureSpec.getSize(heightMeasureSpec);
System.out.println("on measure");
// Satisfy contract by calling setMeasuredDimension
setMeasuredDimension(widthMsSize,
heightMsSize);
}
protected void onDraw(Canvas canvas) {
canvas.drawLine(5, heightMsSize-10, widthMsSize-5, heightMsSize-10, mPaint); //draw underline
canvas.drawLine(8, heightMsSize-10,8, heightMsSize-20, mPaint); //draw left corner
canvas.drawLine(widthMsSize-8, heightMsSize-10,widthMsSize-8, heightMsSize-20, mPaint); //draw right corner
super.onDraw(canvas);
}
}
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.testanimationfadeinfadeout.CustomEditText
android:id="@+id/textedit"
android:layout_width="228dp"
android:layout_height="41dp"
android:ems="10"
android:hint="color is changed" />
</LinearLayout>
0
要自定義編輯文本我做了以下的方法。它對我來說非常簡單。
public class CardNumberText extends EditText {
boolean isFocus;
Paint mPaint;
Rect mRect;
int widthSize, heightSize;
public CardNumberText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initStyle();
}
private void initStyle() {
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.parseColor("#B3B3B3"));
}
public CardNumberText(Context context, AttributeSet attrs) {
super(context, attrs);
initStyle();
}
public CardNumberText(Context context) {
super(context);
initStyle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rect = mRect;
Paint paint = mPaint;
if (isFocus) {
mPaint.setStrokeWidth(3.0f);
mPaint.setColor(Color.parseColor("#80CBC4"));
} else {
mPaint.setStrokeWidth(1.5f);
mPaint.setColor(Color.parseColor("#B3B3B3"));
}
for (int i = 0; i < getLineCount(); i++) {
int baseline = getLineBounds(i, rect);
canvas.drawLine(rect.left, baseline + 20, rect.right, baseline,
paint);
}
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
isFocus = true;
} else {
isFocus = false;
}
}
}
2
public static void setEditTextUnderlineColor(final EditText editText, final int focusedColor, final int unfocusedColor) {
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText.getBackground().setColorFilter(focusedColor, PorterDuff.Mode.SRC_ATOP);
return;
}
editText.getBackground().setColorFilter(unfocusedColor, PorterDuff.Mode.SRC_ATOP);
}
});
editText.getBackground().setColorFilter(unfocusedColor, PorterDuff.Mode.SRC_ATOP);
+0
舊帖子,但仍然很棒 – oga
相關問題
- 1. 改變顏色的EditText的下劃線
- 2. 風格EditText下劃線顏色android
- 3. (材料)的EditText下劃線顏色
- 4. 更改TextView的下劃線顏色
- 5. 更改EditText的顏色android
- 6. 在Edittext中編輯下劃線的顏色Android
- 7. 當輸入正確時更改android autocompletetextview下劃線顏色?
- 8. 更改android中的下劃線的顏色
- 9. Android:是否可以動態更改EditText的邊框顏色?
- 10. 動態更改折線顏色
- 11. 動態更改SherlockActionBar線條顏色
- 12. Android Tab下劃線顏色不變
- 13. 更改editText標籤的顏色,android
- 14. 更改android editText高亮顏色
- 15. 更改光標的顏色android EditText
- 16. RichTextBox和下劃線顏色
- 17. Android:動態更改TextView背景顏色
- 18. Android動態更改進度欄顏色
- 19. 更改edittext backgroundTint顏色
- 20. EditText背景基線顏色根據其焦點更改android
- 21. 如何更改Android中EditText的底線顏色?
- 22. 如何改變下劃線的顏色在android系統
- 23. 棒棒堂:如何更改AutoTextView的下劃線顏色?
- 24. 如何更改Materialise CSS選項卡的下劃線顏色?
- 25. 更改字體的顏色並使其加下劃線
- 26. 更改<a>鏈接下劃線顏色
- 27. 如何更改TextInputLayout下劃線的顏色?
- 28. 用於更改下劃線顏色的jQuery插件?
- 29. 如何更改angular4中md-input-container的下劃線顏色?
- 30. 如何在android中更改EditText泡泡顏色(在光標下)?
哇,感謝名單爲這個快速和樂於助人的帖子! 只是一個(愚蠢)的問題。如果我想在我的項目中動態添加 CustomEditText,我應該爲AttributeSet放置什麼? 美好的夜晚,thanx太多了,對不起英語感到抱歉;) –
再次嗨! AttributeSet是你在xml中聲明的屬性,如android:hint等。 如果你不需要它,你必須使用public CustomEditText(Context context){super(context); ...}作爲你的構造函數 ps:在stackoverflow上沒有愚蠢的問題! :) – Frank
第三次嗨! 我現在有一個新問題。我的Layout沒有任何EditText。我是否必須覆蓋getDraw或使用Layout的某些功能? 我的代碼:'layout =(LinearLayout)findViewById(R.id.main_layout); CustomEditText customET = new CustomEditText(this); // getApplicationContext();也不起作用。 customET.setText(text); customET.setLayoutParams(new LayoutParams( \t LayoutParams.FILL_PARENT, \t LayoutParams.WRAP_CONTENT)); layout.addView(customET);' 你有什麼想法可能是什麼問題? –