Unable to set text in EditText Kotlin

一世执手 提交于 2021-02-10 14:23:03

问题


I'm new to kotlin and i was trying to make a simple plus minus controller, so the layout has two buttons, Plus and Minus and an EditText both are inside a fragment, the issue is that when i'm trying to press one of the buttons the app crash on .setText with error Resuource not found.

So the question is what is the right way to set text in an EditText inside a fragment?

Here is my code:

FirstFragment.tk

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    view.findViewById<ImageButton>(R.id.btnPlus).setOnClickListener {
        changeQta(it)
    }

    view.findViewById<ImageButton>(R.id.btnMinus).setOnClickListener {
        changeQta(it)
    }
}

private fun changeQta(v: View) {
    val txtQta = view?.findViewById<EditText>(R.id.txtQta)
    val curVal = txtQta?.text.toString().toInt()
    when (v.id) {
        R.id.btnMinus -> {
            if (curVal > 1) {
                txtQta?.setText(curVal - 1)
            }
            if (txtQta?.text.toString().toInt() == 1) {
                v.isEnabled = false
            }
        }
        R.id.btnPlus -> {
            txtQta?.setText(curVal + 1)
        }
        else -> return;
    }
}

回答1:


You are passing an int to setText method. setText method is overloaded and it calls the public final void setText(@StringRes int resid) since this method expects an int and that too a string resource but the int you are passing to it is not a valid StringRes so it causes ResourceNotFoundException.

The solution is to convert the int to String and then pass it to the setText method.

yourTextView.setText((curVal - 1).toString())

or

yourTextView.setText("${curVal - 1}")

Whichever way you prefer




回答2:


The problem is that there are a bunch of setText methods (which come from TextView, which is EditText's superclass) - one of them takes a single Int parameter:

setText(int resid) Sets the text to be displayed using a string resource identifier.

So because you're passing in an Int, it assumes that's a resource ID (things like R.id.btnPlus and R.string.coolText are just Ints), but you don't have an ID with that number, which is why it's saying Resource not found.

You want this version:

setText(CharSequence text)

So you need to explicitly cast your integer to a String (which is a type of CharSequence btw)

txtQta?.setText((curVal - 1).toString())

or you could do it in a string if you like

txtQta?.setText("${curVal - 1}")

by the way, when you do things like this:

val curVal = txtQta?.text.toString().toInt()

you need to add the ? at each step, so it skips each time if the value passed along is null, otherwise you might call toString() on null (which is fine) and then toInt() on that (which will crash)

val curVal = txtQta?.text?.toString()?.toInt()

or if you like

val curVal = txtQta?.let { it.text.toString().toInt() }

in both cases curVal will be the nullable type Int? because that expression can return null (that's why you're null checking each step with ?)



来源:https://stackoverflow.com/questions/65739387/unable-to-set-text-in-edittext-kotlin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!