在android系统中经常需要使用到验证,比如验证输入的文本是否非空,邮箱地址是否合法,是否是数字等等。
如果有文本为空,则光标自动跳转到此文本中。
这里简单的介绍一种非空的验证,其他格式的验证,希望能举一反三 增加验证规则即可。
前端main.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name" android:text="姓名:"/> <EditText android:id="@+id/editText_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入文本信息" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/age" android:text="年龄:"/> <EditText android:id="@+id/editText_age" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="请输入年龄"/> <Button android:id="@+id/btnSubmit" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="提交验证"/> </LinearLayout>
当点击提交验证按钮时,则在Activity中进行验证,具体代码如下:
package com.example.fourandroid;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class MainActivity extends Activity {
private EditText editText_name;
private EditText editText_age;
private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
//获取组件信息;
this.editText_name = (EditText) super.findViewById(R.id.editText_name);
this.editText_age = (EditText)super.findViewById(R.id.editText_age);
//给按钮设置单击事件;
this.btnSubmit = (Button)super.findViewById(R.id.btnSubmit);
this.btnSubmit.setOnClickListener(new myBtnSubmitImpl());
}
public class myBtnSubmitImpl implements OnClickListener{
@Override
public void onClick(View v) {
boolean bol = true;
String age = MainActivity.this.editText_age.getText().toString();
String name = MainActivity.this.editText_name.getText().toString();
//实例化对话框;
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("提示信息");
//判断name是否为空;
if(name == null || name.trim().length() == 0 || "".equals(name.trim())){
builder.setMessage("Name不能为空,请输入信息.");
builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
setResult(RESULT_OK);
}
});
//设置焦点信息;
MainActivity.this.editText_name.setFocusable(true);
MainActivity.this.editText_name.setFocusableInTouchMode(true);
MainActivity.this.editText_name.requestFocus();
MainActivity.this.editText_name.requestFocusFromTouch();
builder.show();
//标识此文本框属性为空;
bol = false;
return;
}
//判断address是否为空;
if(age == null || "".equals(age) || age.trim().length() == 0){
builder.setMessage("Age不能为空,请输入信息.");
//给对话框增加一个确定按钮;
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//监听单击事件;
public void onClick(DialogInterface dialog, int whichButton) {
setResult(RESULT_OK);//确定按钮事件 ;
}
}) ;
//设置address焦点;
MainActivity.this.editText_age.setFocusable(true);
MainActivity.this.editText_age.setFocusableInTouchMode(true);
MainActivity.this.editText_age.requestFocus();
MainActivity.this.editText_age.requestFocusFromTouch();
//显示提示框;
builder.show();
//标识此文本框属性为空;
bol = false;
return;
}
if(bol){
//设置提示信息.
Toast.makeText(MainActivity.this, "恭喜你,验证成功", Toast.LENGTH_LONG).show();
}
}
}
}
效果如下: