我的英文不好道歉,我使用谷歌翻译。
我要创建在用户必须创建一个新的配置文件的活动。 我把限制编辑的15个字符的文本,我想,如果新的配置文件名称有空格或特殊字符显示警告。随着在线视频游戏
下面code帮我检测的空间,而不是特殊字符。
我需要帮助找出特殊字符和响应显示警告。
@覆盖
公共无效的onClick(视图v){
// Convertimos埃尔contenido恩La Caja旅馆德texto恩联合国字符串
。串农布雷= nombreUsuario.getText()的toString();
//思玉野市报德尔字符串ES IGUAL 0,阙ES ES罗MISMO阙dijeramos思ESTA vacio
如果(nombre.length()== 0){
// Creamos埃尔说Aviso
吐司说Aviso = Toast.makeText(getApplicationContext(),POR青睐介绍联合国农布雷迪奥斯Usuario,Toast.LENGTH_LONG);
aviso.show();
}否则如果(nombre.contains()| nombre.contains(\\ W)){
吐司说Aviso = Toast.makeText(getApplicationContext(),没有儿子permitidos洛杉矶espacios妮洛杉矶caracteres ESPECIALES,Toast.LENGTH_LONG);
aviso.show();
} 其他 {
。NOMBRE = nombreUsuario.getText()的toString();
// Conectamos CON LA基地去DATOS
// Creamos未bojetoŸ卤味iniciamos精读新
Plantilla ENTRADA =新Plantilla(CrearUsuarioActivity.this);
entrada.abrir();
// creamos取消方法方法对escribir恩拉基地去DATOS(CREAR入住日期)
entrada.crearEntrada(农布雷);
entrada.cerrar();
}
}
解决方案
您可以使用:
string.matches([A-ZA-Z?] *)
这将评估为true,如果每个字符的字符串或者是一个小写字母AZ,大写字母AZ,句号,问号,或一个空格。
这样的:
公共无效点击(视图v){
如果(v.getId()== R.id.button1){
字符串NOMBRE = textMessage.getText()的toString()。
如果(nombre.length()== 0){
// Creamos埃尔说Aviso
吐司说Aviso = Toast.makeText(getApplicationContext()
POR青睐介绍联合国农布雷迪奥斯Usuario
Toast.LENGTH_LONG);
aviso.show();
}否则,如果(!nombre.matches([A-ZA-Z?] *)){
吐司说Aviso =吐司
.makeText(
getApplicationContext(),
没有儿子permitidos洛杉矶espacios妮洛杉矶caracteres ESPECIALES
Toast.LENGTH_LONG);
aviso.show();
} 其他 {
// 做你想做的
}
}
}
[A-ZA-Z0-9?] *?
An apology for my bad English, I'm using google translate.
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
The following code helps me to detect spaces, but not special characters.
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();
}
}
解决方案
You can use:
string.matches("[a-zA-Z.? ]*")
That will evaluate to true if every character in the string is either a lowercase letter a-z, an uppercase letter A-Z, a period, a question mark, or a space.
like:
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
}
}
}
"[a-zA-Z0-9.? ]*"