android 验证二
<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>
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(); } } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。