<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="Name:"/> <edittext android:id="@+id/edittext_name"Android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:hint="Please enter text information"/> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:id="@+id/age"Android:text="Age:"/> <edittext android:id="@+id/edittext_age"Android:layout_height="wrap_content"Android:layout_width="fill_parent"Android:hint="Please enter your age"/> <button android:id="@+id/btnsubmit"Android:layout_height="wrap_content"Android:layout_width="wrap_content"Android:text="Submit Validation"/> </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 classMainactivity extends Activity {PrivateEditText Edittext_name; PrivateEditText Edittext_age; PrivateButton btnsubmit; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Super.setcontentview (R.layout.activity_main); //get component information; This. Edittext_name =(EditText) Super.findviewbyid (r.id.edittext_name); This. Edittext_age =(EditText) Super.findviewbyid (r.id.edittext_age); //sets the Click event to the button; This. btnsubmit =(Button) Super.findviewbyid (r.id.btnsubmit); This. Btnsubmit.setonclicklistener (NewMybtnsubmitimpl ()); } Public classMybtnsubmitimpl implements onclicklistener{@Override Public voidOnClick (View v) {Boolean Bol=true; String Age= Mainactivity. This. Edittext_age.gettext (). toString (); String name= Mainactivity. This. Edittext_name.gettext (). toString (); //Instantiate dialog box; Alertdialog.builder Builder =NewAlertdialog.builder (V.getcontext ()); Builder.settitle ("Prompt Information"); //determine if name is empty; if(Name = =NULL|| Name.trim (). Length () = =0||"". Equals (Name.trim ())) {Builder.setmessage ("name cannot be empty, please enter the information."); Builder.setpositivebutton ("Confirm",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Setresult (RESULT_OK); } }); //set focus information; Mainactivity. This. edittext_name.setfocusable (true); Mainactivity. This. Edittext_name.setfocusableintouchmode (true); Mainactivity. This. Edittext_name.requestfocus (); Mainactivity. This. Edittext_name.requestfocusfromtouch (); Builder.show (); //identifies this text box property as empty; Bol =false; return; } //determine if the address is empty; if(age = =NULL||"". Equals (age) | | Age.trim (). Length () = =0) {builder.setmessage ("Age cannot be empty, please enter information."); //Add a OK button to the dialog box; Builder.setpositivebutton ("Determine",NewDialoginterface.onclicklistener () {//listen for click events; Public voidOnClick (Dialoginterface Dialog,intWhichbutton) {Setresult (RESULT_OK);//determining button events; } }) ; //set address focus; Mainactivity. This. edittext_age.setfocusable (true); Mainactivity. This. Edittext_age.setfocusableintouchmode (true); Mainactivity. This. Edittext_age.requestfocus (); Mainactivity. This. Edittext_age.requestfocusfromtouch (); //display the prompt box; builder.show (); //identifies this text box property as empty; Bol =false; return; } if(BOL) {//sets the prompt information. Toast.maketext (mainactivity. This,"Congratulations, the verification is successful.", Toast.length_long). Show (); } } } }
Android authentication Two