2012-11-05 37 views

Trả lời

4

Tạo riêng tùy chỉnh kiểm soát EditText bạn

Dưới đây là một ví dụ mà tôi thực hiện chỉ dành cho bạn:

Khi được chọn, bạn Juste phải thay đổi mPaint.setColor (Color.GREEN); khác màu

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

Wow, Thanx cho bài đăng nhanh và hữu ích này! Chỉ một câu hỏi nữa (ngu ngốc). Nếu tôi muốn thêm CustomEditText động trong dự án của tôi, tôi nên đặt gì cho AttributeSet? Chào buổi tối, thanx rất nhiều và xin lỗi vì tiếng anh xấu;) –

+0

xin chào một lần nữa! AttributeSet là các thuộc tính mà bạn đã khai báo bên trong xml của bạn như android: hint,… Nếu bạn không cần nó, bạn phải sử dụng CustomEditText (ngữ cảnh bối cảnh) {super (context); ...} làm hàm tạo của bạn ps: không có câu hỏi ngu ngốc nào trên stackoverflow! :) – Frank

+0

Xin chào lần thứ ba! Tôi đã gặp sự cố mới ngay bây giờ. Tôi không nhận được bất kỳ EditText nào trên Bố cục của tôi. Tôi có phải ghi đè lên một số chức năng như getDraw hoặc đôi khi sử dụng Bố cục không? Mã của tôi: 'layout = (LinearLayout) findViewById (R.id.main_layout); CustomEditText customET = new CustomEditText (điều này); // getApplicationContext(); không làm việc quá. customET.setText (văn bản); customET.setLayoutParams (LayoutParams mới ( \t LayoutParams.FILL_PARENT, \t LayoutParams.WRAP_CONTENT)); layout.addView (customET); ' Bạn có bất kỳ ý tưởng nào có thể là vấn đề không? –

0

Để tùy chỉnh chỉnh sửa văn bản tôi đã làm theo cách sau. Nó làm việc cho tôi cách khá đơn giản.

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

Bài đăng cũ nhưng vẫn tuyệt vời – oga

Các vấn đề liên quan