In questo articolo vedremo come verificare se una stringa di testo alfanumerica che abbiamo inserito in una EditText è in formato esadecimale.
activity_main.xml
Questa è la funzione che esegue il controllo:
Ecco come si presenta l'intero codice nel file MainActivity.kt
activity_main.xml
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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity" | |
android:orientation="vertical"> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:inputType="textPersonName" | |
android:ems="10" | |
android:id="@+id/editText" | |
android:hint="inserire una stringa esadecimale"/> | |
<Button | |
android:text="stringa HEX?" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/button1"/> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/textView" | |
android:textSize="18sp" | |
android:textColor="#000000"/> | |
</LinearLayout> |
Questa è la funzione che esegue il controllo:
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
//funzione che verifica se una stringa alfanumerica è in formato esadecimale | |
private val HEXADECIMAL_PATTERN = compile("\\p{XDigit}+") | |
private fun isHexadecimal(input: String): Boolean { | |
val matcher = HEXADECIMAL_PATTERN.matcher(input) | |
return matcher.matches() | |
} |
Ecco come si presenta l'intero codice nel 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.tutorialishex | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.Button | |
import android.widget.EditText | |
import android.widget.TextView | |
import java.util.regex.Pattern.compile //importazione della Classe: Pattern | |
class MainActivity : AppCompatActivity() { | |
var editStr: EditText? = null | |
var bnExtract: Button? = null | |
var txtHEX: TextView? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
editStr = findViewById(R.id.editText) | |
bnExtract = findViewById(R.id.button1) | |
txtHEX = findViewById(R.id.textView) | |
bnExtract!!.setOnClickListener { | |
val edit = editStr!!.text.toString() | |
//se non è stato scritto alcun carattere nella EditText azzera la TextView | |
if(edit == "") | |
{ | |
txtHEX!!.text ="" | |
} | |
//altrimenti viene eseguito il controllo | |
else | |
{ | |
//chiamata della funzione isHexadecimal con passaggio della variabile edit | |
if (isHexadecimal(edit)) { | |
//insierire qui il codice se i caratteri inseriti sono in formato esadecimale | |
txtHEX!!.text = "Il valore inserito è un formato esadecimale valido" | |
} else { | |
//insierire qui il codice se i caratteri inseriti non sono in formato esadecimale | |
txtHEX!!.text = "Il valore inserito NON è un formato esadecimale valido" | |
} | |
} | |
} | |
} | |
//funzione che verifica se una stringa alfanumerica è in formato esadecimale | |
private val HEXADECIMAL_PATTERN = compile("\\p{XDigit}+") | |
private fun isHexadecimal(input: String): Boolean { | |
val matcher = HEXADECIMAL_PATTERN.matcher(input) | |
return matcher.matches() | |
} | |
} |
Download Project | ![]() |
Download file APK | ![]() |
Commenti
Posta un commento