Zacharie Roy
Zacharie Roy

Reputation: 45

How to use KeyEvent in Kotlin

This my first kotlin/java code. I want learn from this.

When I push "OK" button of Android keyboard, I want my app do same thing as when I push my button with android:onClick="onAnswerClick"

I know my code is not very good. You can't help me to know how to optimize it

I don't know if its a good idea to learn to code on android with kotlin.

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.*
import android.widget.AdapterView
import android.widget.Toast
import android.widget.Spinner
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.EditText



class MainActivity : Activity() {

    class Calculateurs{
        fun aire(r: Double): Double{
            return 3.141592 * r * r
        }
        fun circonference(r: Double): Double{
            return 2.0 * 3.141592 * r
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val spinner = findViewById<Spinner>(R.id.spinner) as Spinner

        // Create an ArrayAdapter using the string array and a default spinner layout
        val adapter = ArrayAdapter.createFromResource(this,
                R.array.mesure_array, android.R.layout.simple_spinner_item)
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        // Apply the adapter to the spinner
        spinner.adapter = adapter


        }


    @SuppressLint("SetTextI18n")
    fun onAnswerClick(view: View) {

        val answer = (findViewById<EditText>(R.id.editText2) as EditText).text.toString()
        val spinner = findViewById<View>(R.id.spinner) as Spinner

        var rayon = java.lang.Double.parseDouble(answer)

        if (spinner.getSelectedItem() == "Diamètre"){
            rayon = rayon *2
        }


        if (rayon < 0) {
            val toasty = Toast.makeText(applicationContext, "Ce nombre est négatif !", Toast.LENGTH_SHORT)
            toasty.show()
        } else if (rayon > 0) {
            val c = Calculateurs()
            val tc = findViewById<TextView>(R.id.cire) as TextView
            tc.text = "" + c.circonference(rayon)
            val ta = findViewById<TextView>(R.id.aire) as TextView
            ta.text = "" + c.aire(rayon)
        } else {
            val toasty = Toast.makeText(applicationContext, "Ce nombre est nul !", Toast.LENGTH_SHORT)
            toasty.show()
        } 
    }
}

Upvotes: 0

Views: 5644

Answers (1)

nPn
nPn

Reputation: 16748

I think your question is how do you get your app to respond to the enter or done key when you press it in an EditText.

Since you did not show your layout, I will make a few assumptions.

Lets say you have and EditText that has the following attributes (plus others)

<EditText
    android:id="@+id/field"
    android:imeOptions="actionDone"
    android:inputType="text"
    ... 
    app:layout_constraintTop_toTopOf="parent" />

Here is an example activity that will respond to hitting the enter key.

class MainActivity : AppCompatActivity() {

lateinit var field: EditText

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    field = findViewById(R.id.field) as EditText
}

override fun onResume() {
    super.onResume()
    field.setOnEditorActionListener( { textView, action, event ->
        var handled = false
        if (action == EditorInfo.IME_ACTION_DONE) {
            println("call your method here")
            handled = true
        }
        handled
    })
}

You can call your onAnswerClick() in the location of the println("call your method here") statement, passing in textView if you like.

This example makes use of Kotlin's SAM Conversion which is covered in this section of the Kotlin documentation, specifically it uses the SAM conversion to create a TextView.OnEditorActionListener from a lambda expression

As Kamil Kulig, points out, there is definitely more samples and examples using Java than Kotlin, but I don't think you have to start with Java. You will need to learn enough Java to be able to understand what's going on in examples and documentation however.

Upvotes: 3

Related Questions