In questo articolo vedremo come eseguire una serie di funzioni quando si clicca sul pulsante DONE (Conferma) della tastiera.
Prima di proseguire, leggere questo articolo su come modificare la tastiera.
Questo è il codice da inserire all'interno della funzione onCreate che permette tale funzione:
Ecco il codice completo all'interno del file MainActivity.kt
Prima di proseguire, leggere questo articolo su come modificare la tastiera.
Questo è il codice da inserire all'interno della funzione onCreate che permette tale funzione:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
editText!!.setOnEditorActionListener { v, actionId, event -> | |
if (actionId == EditorInfo.IME_ACTION_DONE || | |
event?.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) { | |
//inserire qui il codice da eseguire quando si clicca sul tasto DONE della tastiera | |
//non cancellare questa riga di codice | |
true | |
} | |
else | |
{ | |
//non cancellare questa riga di codice | |
false | |
} | |
} |
Ecco il codice completo all'interno del file MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dm.tutorialtext8 | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.view.KeyEvent //importazione della Classe: KeyEvent | |
import android.view.inputmethod.EditorInfo //importazione della Classe: inputmethod EditorInfo | |
import android.widget.EditText | |
import android.widget.TextView | |
class MainActivity : AppCompatActivity() { | |
var editName: EditText? = null | |
var editSurname: EditText? = null | |
var editAge: EditText? = null | |
var txtAge: TextView? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
editName = findViewById(R.id.txtNome) | |
editSurname = findViewById(R.id.txtCognome) | |
editAge = findViewById(R.id.txtEta) | |
txtAge = findViewById(R.id.textView) | |
editName!!.setImeOptions(EditorInfo.IME_ACTION_NEXT) | |
editSurname!!.setImeOptions(EditorInfo.IME_ACTION_NEXT) | |
editAge!!.setImeOptions(EditorInfo.IME_ACTION_DONE) | |
editAge!!.setOnEditorActionListener { v, actionId, event -> | |
if (actionId == EditorInfo.IME_ACTION_DONE || | |
event?.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) { | |
//inserire qui il codice da eseguire quando si clicca sul tasto DONE della tastiera | |
if(editAge!!.text.toString() == "") | |
{ | |
txtAge!!.text = "" | |
} | |
else | |
{ | |
if (editAge!!.text.toString().toInt() < 18) | |
{ | |
txtAge!!.text = "Non sei maggiorenne" | |
} | |
else | |
{ | |
txtAge!!.text = "Sei maggiorenne" | |
} | |
} | |
//non cancellare questa riga di codice | |
true | |
} | |
else | |
{ | |
//non cancellare questa riga di codice | |
false | |
} | |
} | |
} | |
} |
Download Project | ![]() |
Download file APK | ![]() |
Commenti
Posta un commento