Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the text of an EditText to an Int?

In an attempt to set an EditText to an Int value, I've tried various ways of converting the Int to a value that the EditText will accept, but all fail:

   processButton.setOnClickListener {
        var intNo = inputText.text as Int
        intNo *= 2
        outputText.text = intNo as String       // error = "required editable"
        outputText.text = intNo.toString()      // err: type mismatch 
        outputText.text = Int.toString(intNo)   // type mismatch reqd editable
        outputText.text = "What is going on?"   // type mismatch reqd editable
    }

How can I set the EditText to an Int value?

like image 296
Carlton McDonald Avatar asked Oct 03 '17 03:10

Carlton McDonald


3 Answers

Try given code it will work. What I am doing here I am converting inputText first to String then Int. After I am multiplying with 2 then I am assigning a value for outputText by converting to string.

processButton.setOnClickListener {

    var intNo  = inputText.text.toString().toInt()
    intNo *= 2
    //println(intNo.toString())
    val myString = intNo.toString()
    // If you using outputText as Editable then use this
    outputText.text = SpannableStringBuilder(myString)

    }
like image 163
Rajesh Dalsaniya Avatar answered Oct 23 '22 15:10

Rajesh Dalsaniya


 var a: Int = 12
 var s: String = a.toString()

This should work for this.

like image 18
Ankit Kumar Avatar answered Oct 23 '22 15:10

Ankit Kumar


There are a couple things going on here, and to understand them, let's take a look at the various getText and setText methods that EditText has:

Editable getText()
void setText(CharSequence text)
void setText(@StringRes int resid)
// many other setText methods with buffer options

So what Kotlin does here to let you use property syntax is that it creates a text property. The getter used for the property is obvious, since there's only one. The setter for the property is supposed to be the one that takes a CharSequence parameter (it would make sense, Editable extends CharSequence), but actually trying to assign anything other than an Editable to it won't work. See this issue.


To get to the problem at hand, you can read the value in your EditText and convert it to a String like this:

val input = inputText.text.toString()

Then, you can use the toInt() function from the standard library to convert it to an Int (be aware that this will throw an exception if the String can't be parsed):

val doubled = input.toInt() * 2

And finally, you can set the value of the EditText by calling the setText setter in the traditional Java style, passing in a String:

inputText.text.setText(doubled.toString())

A bit of a mess because of the two-way conversion between String and Int, plus the oddities of how the text property is generated here, but that's the way to do it. If you're bothered by how this looks, you could always hide some of this mechanism behind extension properties.

like image 3
zsmb13 Avatar answered Oct 23 '22 14:10

zsmb13