0

我是初學者。我想爲自定義EditText定義xml,然後在運行時以編程方式添加這些自定義edittext,而無需使用一堆代碼來自定義editTexts。這可能類似於從庫中實現自定義按鈕,textviews等...雖然這將是我自己的。解決這個問題的最好方法是什麼?定義自定義EditText

謝謝!

+0

定義你的自定義類的EditText這是擴展EditText檢查:https://alinberce.wordpress.com/2012/02/20/android-edittext-with-custom-font-and-clear-button/ – 2014-11-25 04:27:12

+0

你的意思是你需要添加自定義設計? – 2014-11-25 04:30:40

+0

<?xml version =「1.0」encoding =「utf-8」?> <固體機器人:顏色= 「#FFFFFF」/> <拐角 機器人:bottomLeftRadius = 「10dp」 機器人:bottomRightRadius = 「10dp」 機器人:topLeftRadius = 「10dp」 android:topRightRadius =「10dp」/> 2014-11-25 04:33:04

回答

3
除了上述評論鏈接共享代碼

替代代碼如下: 基本上此代碼便於用戶自定義字體等

public class MyEditText extends EditText { 

    public MyEditText(Context context) { 
     super(context); 
    } 

    public MyEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     parseAttributes(context, attrs); 
    } 

    public MyEditText(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     parseAttributes(context, attrs); 
    } 

    private void parseAttributes(Context context, AttributeSet attrs) { 
     TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.Simplified); 

     int typefaceValue = values.getInt(R.styleable.Simplified_typeface, 0); 
     values.recycle(); 

     setTypeface(MyFontUtil.obtaintTypeface(context, typefaceValue)); 
    } 

} 

XML

<com.my.mtetno.widget.MyEditText 
    android:id="@+id/uname" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/lh_edit_field_without_border" 
    android:inputType="textEmailAddress" 
    android:maxLines="1" 
    android:overScrollMode="always" 
    android:textSize="@dimen/login_page_edit_text_size" 
    app:typeface="simplified_regular" /> 
+0

如果你想了解更多abt在我的代碼中使用的AttributeSet這是最好的鏈接 - > http://stackoverflow.com/questions/2695646/declaring-a-custom-機器人-UI-元件使用的XML – KOTIOS 2014-11-25 04:36:44