問(wèn)題描述
抱歉我的英語(yǔ)不好,我正在使用谷歌翻譯.
An apology for my bad English, I'm using google translate.
我正在創(chuàng)建一個(gè)用戶(hù)必須在其中創(chuàng)建新個(gè)人資料的活動(dòng).我限制編輯 15 個(gè)字符的文本,如果新的配置文件名稱(chēng)包含空格或特殊字符,我希望顯示警告.作為在線視頻游戲
I'm creating an activity in which users must create a new profile. I put a limit to edit text of 15 characters and I want that if the new profile name has spaces or special characters display a warning. As online video games
以下代碼可以幫助我檢測(cè)空格,但不能檢測(cè)特殊字符.
The following code helps me to detect spaces, but not special characters.
我需要幫助來(lái)識(shí)別特殊字符并顯示警告作為響應(yīng).
I need help to identify special characters and display a warning in response.
@Override
public void onClick(View v) {
//Convertimos el contenido en la caja de texto en un String
String nombre = nombreUsuario.getText().toString();
//Si el tama?o del String es igual a 0, que es es lo mismo que dijeramos "Si esta vacio"
if (nombre.length() == 0) {
//Creamos el aviso
Toast aviso = Toast.makeText(getApplicationContext(), "Por favor introduce un nombre de Usuario", Toast.LENGTH_LONG);
aviso.show();
} else if (nombre.contains(" ") | nombre.contains("\W")) {
Toast aviso = Toast.makeText(getApplicationContext(), "No son permitidos los espacios ni los caracteres especiales", Toast.LENGTH_LONG);
aviso.show();
} else {
nombre = nombreUsuario.getText().toString();
//Conectamos con la base de datos
//Creamos un bojeto y lo iniciamos con new
Plantilla entrada = new Plantilla(CrearUsuarioActivity.this);
entrada.abrir();
//creamos un metodo para escribir en la base de datos (crear entradas)
entrada.crearEntrada(nombre);
entrada.cerrar();
}
}
推薦答案
你可以使用:
string.matches("[a-zA-Z.? ]*")
如果字符串中的每個(gè)字符都是小寫(xiě)字母 a-z、大寫(xiě)字母 A-Z、句點(diǎn)、問(wèn)號(hào)或空格,則計(jì)算結(jié)果為 true.
喜歡:
public void Click(View v) {
if (v.getId() == R.id.button1) {
String nombre = textMessage.getText().toString();
if (nombre.length() == 0) {
// Creamos el aviso
Toast aviso = Toast.makeText(getApplicationContext(),
"Por favor introduce un nombre de Usuario",
Toast.LENGTH_LONG);
aviso.show();
} else if (!nombre.matches("[a-zA-Z.? ]*")) {
Toast aviso = Toast
.makeText(
getApplicationContext(),
"No son permitidos los espacios ni los caracteres especiales",
Toast.LENGTH_LONG);
aviso.show();
} else {
// Do what ever you want
}
}
}
這篇關(guān)于如何檢測(cè)編輯文本中的特殊字符并顯示 Toast 作為響應(yīng)(Android)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!