Extending a EditText in Android. What am I doing wrong?

后端 未结 3 1606
无人及你
无人及你 2021-01-04 06:43

So I\'m trying to get a grasp of using custom controls in Android. But my app crashes on trying to create the activity. Here\'s the code:

package com.myApp;
         


        
3条回答
  •  长情又很酷
    2021-01-04 07:08

    import android.annotation.TargetApi;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Typeface;
    import android.os.Build;
    import android.util.AttributeSet;
    
    
    
    
    /**
     * Created by rohann on 14/07/2016.
     */
    public class LightEditText extends android.widget.EditText{
    
        public LightEditText(Context context) {
            super(context);
            setFont();
    
        }
    
        public LightEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            setFont();
        }
    
        public LightEditText(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            setFont();
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public LightEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            setFont();
        }
    
        /**
         * This method is used to set the given font to the TextView.
         */
        private void setFont() {
            Typeface typeface = TypefaceCache.get(getContext().getAssets(), "fonts/Roboto-Light.ttf");
            setTypeface(typeface);
        }
    
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
        }
    }
    

提交回复
热议问题