Skip to content Skip to sidebar Skip to footer

View Extending Edittext Loses Styles And Is Not Focusable

I try to subclass EditText for convenience reasons (NumberEdit) using kotlin but the rendered View loses most of the EditText properties. The look is that of a TextView and it is n

Solution 1:

I'm not a kotlin expert but if you look at the java source code for edittext you have following:

publicclassEditTextextendsTextView {
    publicEditText(Context context) {
        this(context, null);
    }
    publicEditText(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.editTextStyle);
    }
    publicEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }
    publicEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

It doesn't look like you pass the right parameters to the constructor... You pass a lot of 0s and nulls...

Solution 2:

In Kotlin you can write this much more concise:

classEditNumber@JvmOverloadsconstructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = R.attr.editTextStyle,
    defStyleRes: Int = 0
) : EditText(context, attrs, defStyle, defStyleRes)

Notice the defStyle parameter.

Post a Comment for "View Extending Edittext Loses Styles And Is Not Focusable"