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