Android Studio无法在布局编辑器中呈现自定义视图

时间:2018-10-02 09:41:11

标签: android android-studio android-custom-view

我正在创建一个CustomView,它基本上包装了declare @tmp table (Ric varchar(50), Charge numeric(10,3), [Date] Date) insert into @tmp values ('VOD.L', 2 , '20180601') ,('VOD.L', 5 , '20181002') ,('VOD.L', 4.5, '20180212') ,('RBS.L', 3 , '20180504') ,('RBS.L', 6 , '20180708') select t.Ric, t.Charge, t.[Date] from ( select ric, Charge, row_number() over (partition by ric order by [Date] desc) as rn, [Date] from @tmp ) t where rn = 1 TextInputLayout。 Android Studio中的布局编辑器无法显示此自定义视图的预览。我已经尝试过构建项目,将其清理干净,但仍然无法显示预览。这是相同的Kotlin文件。

TextInputEditText

这是布局文件:

class PrimaryEditText : LinearLayout {
    private var textInputLayout: TextInputLayout
    private var textInputEditText: TextInputEditText
    private var onTextChangeListener: ((String) -> Unit)? = null

    constructor(context: Context) : this(context, null, 0)

    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        val inflater = LayoutInflater.from(context)
        val rootView = inflater.inflate(R.layout.primary_edit_text, this)
        textInputLayout = rootView.findViewById(R.id.textInputLayout)
        textInputEditText = rootView.findViewById(R.id.textInputEditText)

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.PrimaryEditText)
        initAttrs(typedArray)
        typedArray.recycle()

        // Add TextWatcher to listen to text
        textInputEditText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {}
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                onTextChangeListener?.invoke(s?.toString() ?: "")
            }
        })
    }

    /*
    * Initialize the attributes
    * */
    private fun initAttrs(typedArray: TypedArray) {
        val inputType = typedArray.getInteger(R.styleable.PrimaryEditText_android_inputType, InputType.TYPE_CLASS_TEXT)
        textInputEditText.inputType = inputType

        textInputLayout.hint = typedArray.getString(R.styleable.PrimaryEditText_hint)
    }

    /**
     * Set and get the text entered by the user
     * */
    var inputText: String
        get() = textInputEditText.text.toString()
        set(value) {
            textInputEditText.setText(value)
        }

    /**
     * Set and get the error on TextInputLayout
     * */
    var error: String
        get() = textInputLayout.error.toString()
        set(value) {
            textInputLayout.error = value
        }

    /**
     * Add a listener for text change
     * */
    fun setOnTextChangeListener(listener: (String) -> Unit) {
        this.onTextChangeListener = listener
    }
}

0 个答案:

没有答案