2012-11-05 15 views
7

से अंडरलाइन रंग बदलें, क्या गतिशील रूप से एडिटटेक्स्ट से अंडरलाइन का रंग बदलना संभव है? प्रभाव की तरह, जब मैं इसे फोकस करता हूं, तो यह नीला हो जाता है।एंड्रॉइड, एक एडिटटेक्स्ट से गतिशील रूप से

आशा है कि आप समझें कि मैं क्या करना चाहता हूं।

उत्तर

4

यहाँ एक उदाहरण है कि मैं आपके लिए बनाया गया है अपने खुद के अनुकूलित EditText नियंत्रण

बनाएँ:

चुने जाने पर, आप juste 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

वाह, यह तेजी से और उपयोगी पोस्ट के लिए thanx! बस एक और (बेवकूफ) सवाल। अगर मैं अपने प्रोजेक्ट में गतिशील रूप से CustomEditText जोड़ना चाहता हूं, तो मुझे एट्रिब्यूटसेट के लिए क्या रखना चाहिए? अच्छी शाम, thanx इतना बुरा और बुरा अंग्रेजी के लिए खेद है;) –

+0

हाय फिर से! एट्रिब्यूटसेट उन गुणों को है जिन्हें आपने अपने एक्सएमएल जैसे एंड्रॉइड: इशारा, इत्यादि के अंदर घोषित किया है। यदि आपको इसकी आवश्यकता नहीं है, तो आपको सार्वजनिक CustomEditText (संदर्भ संदर्भ) {सुपर (संदर्भ) का उपयोग करना होगा; ...} आपके कन्स्ट्रक्टर ps के रूप में: stackoverflow पर कोई बेवकूफ सवाल नहीं है! :) – Frank

+0

तीसरे बार हाय! मुझे अब एक नई समस्या मिली है। मुझे अपने लेआउट पर कोई संपादन टेक्स्ट नहीं मिला है। क्या मुझे कुछ फ़ंक्शन को ओवरराइट करना है जैसे GetDraw या कभी-कभी लेआउट का उपयोग करता है? मेरा कोड: 'लेआउट = (लीनियरलाउट) findViewById (R.id.main_layout); CustomEditText customET = नया CustomEditText (यह); // getApplicationContext(); भी काम नहीं करता है। customET.setText (टेक्स्ट); customET.setLayoutParams (नया लेआउट पैराम ( \t लेआउट पैराम्स.फिल_PARENT, \t लेआउटपैम्स .RAP_CONTENT)); layout.addView (customET); ' क्या आपको कोई जानकारी है कि समस्या क्या हो सकती है? –

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