视图扩展 EditText 失去样式并且不可聚焦 [英] View extending EditText loses Styles and is not focusable

查看:23
本文介绍了视图扩展 EditText 失去样式并且不可聚焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于方便原因(NumberEdit),我尝试使用 kotlin 对 EditText 进行子类化,但渲染的 View 丢失了大部分 EditText 属性.外观是 TextView 的外观,它不能用鼠标(在模拟器中)聚焦.当我点击活动时,我可以编辑第一个 NumberEdit 小部件,并可以使用 Tab 键循环到下一个.

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 not focusable with the mouse (in the emulator). When I click into the activity I can then edit the first of the NumberEdit widgets and can cycle to the next one with the tab key.

我添加了两个模拟器屏幕截图来说明差异.

I added two emulator screenshots to illustrate the difference.

EditText 看起来像这样

An EditText looks like this

新的 NumberEdit 看起来像这样

The new NumberEdit looks like this

扩展类如下所示:

import android.content.Context
import android.text.InputType
import android.util.AttributeSet
import android.widget.EditText


class EditNumber(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int, defStyleRes: Int)
    : EditText(context, attributeSet, defStyleAttr, defStyleRes) {

    constructor(context: Context) : this(context, null, 0, 0)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0, 0)
    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int)
        : this(context, attributeSet, defStyleAttr, 0)

    init {
        inputType = InputType.TYPE_CLASS_NUMBER + InputType.TYPE_NUMBER_FLAG_DECIMAL
    }
}

有人知道我做错了什么吗?我是否必须显式引用某些属性?

Does anyone have a clue what I am doing wrong? Do I have to reference some attributes explicitly?

推荐答案

我不是 kotlin 专家,但如果您查看 edittext 的 Java 源代码,您会看到以下内容:

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

public class EditText extends TextView {
    public EditText(Context context) {
        this(context, null);
    }
    public EditText(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.editTextStyle);
    }
    public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }
    public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

看起来您没有将正确的参数传递给构造函数……您传递了很多 0 和空值……

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

这篇关于视图扩展 EditText 失去样式并且不可聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆