In questo articolo vedremo come poter estrarre una stringa di testo da una EditText.
Ecco come si presenta il file activity_main.xml per tutti gli esempi riportati qui sotto.
ESEMPIO 1:
In questo esempio viene estratto il testo a partire dal primo carattere fino al secondo carattere
Questo vale sia per lettere che per numeri
ESEMPIO 2:
In questo esempio viene estratto il testo a partire dal secondo carattere in poi
Questo vale sia per lettere che per numeri
Ecco come si presenta il file activity_main.xml per tutti gli esempi riportati qui sotto.
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"/> | |
<Button | |
android:text="Estrai" | |
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> |
ESEMPIO 1:
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
//dichiarazione variabile per recuperare il testo inserito nella EditText | |
val edit = editStr!!.text.toString() | |
//estrazione della stringa di testo a partire dalla posizione 0 alla posizione 2 | |
txtExtract!!.text = edit.substring(0,2) |
Download Project | ![]() |
Download file APK | ![]() |
ESEMPIO 2:
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
//dichiarazione variabile per recuperare il testo inserito nella EditText | |
val edit = editStr!!.text.toString() | |
//estrazione della stringa di testo a partire dalla posizione 2 | |
txtExtract!!.text = edit.substring(2) |
Commenti
Posta un commento