- Android編程經(jīng)典200例
- 楚無(wú)咎編著
- 1362字
- 2018-12-30 05:08:25
實(shí)例10 簡(jiǎn)單的本地驗(yàn)證
【實(shí)例描述】
在本小節(jié)中介紹簡(jiǎn)單的登錄界面——EditText編輯框的應(yīng)用,在該界面中,若輸入正確的用戶名和密碼,單擊“確定”按鈕,將出現(xiàn)一個(gè)Toast提示“恭喜您登錄成功!”,否則將提示“請(qǐng)輸入正確的用戶名或密碼!”。單擊“清空”按鈕,則會(huì)清空所填寫(xiě)的姓名和密碼內(nèi)容。
本實(shí)例的運(yùn)行效果圖,如圖2-10所示。
提示:在該界面中,實(shí)現(xiàn)了對(duì)用戶名和密碼的驗(yàn)證功能,如圖2-10所示。
【實(shí)現(xiàn)過(guò)程】
本程序的開(kāi)發(fā)主要運(yùn)用了EditText編輯框的相關(guān)知識(shí)。EditText編輯框中可以輸入內(nèi)容,當(dāng)用戶名和密碼正確的時(shí)候,單擊“確定”按鈕就會(huì)彈出Toast提示“恭喜您登錄成功!”,否則將提示“請(qǐng)輸入正確的用戶名或密碼!”。單擊“清空”按鈕,則會(huì)清空所填寫(xiě)的姓名和密碼
在本小節(jié)中通過(guò)對(duì)EdiText編輯框的應(yīng)用,構(gòu)建了一個(gè)簡(jiǎn)單的登錄界面——EditText編輯框。并通過(guò)本程序的實(shí)現(xiàn),來(lái)為讀者介紹EditText編輯框的具體應(yīng)用。內(nèi)容。
在該界面開(kāi)發(fā)中,還用到了TextView文本框控件、單行EditText控件與Button按鈕控件。

圖2-10 EditText編輯框
【代碼解析】
首先為讀者介紹本程序的主界面main.xml的開(kāi)發(fā),代碼如下。
代碼位置:見(jiàn)隨書(shū)光盤(pán)中源代碼/第2章/Sample2_10/ res/layout目錄下的main.xml。
1 <?xml version="1.0" encoding="utf-8"?> <!--版本號(hào)和編碼方式--> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > <!--LinearLayout--> 7 <LinearLayout 8 android:orientation="vertical" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:background="#ffcc66" 12 android:paddingLeft="5dip" 13 android:paddingRight="5dip" 14 android:paddingTop="5dip"> 15 <LinearLayout 16 android:id="@+id/LinearLayout01" 17 android:orientation="horizontal" 18 android:layout_width="fill_parent" 19 android:layout_height="fill_parent"> <!--LinearLayout--> 20 <TextView 21 android:text="用戶名:" 22 android:id="@+id/TextView02" 23 android:textColor="#222222" 24 android:layout_width="wrap_content" 25 android:layout_height="40dip" 26 android:layout_marginLeft="5dip" 27 android:textSize="18dip" 28 android:gravity="center_vertical"> 29 </TextView> <!--TextView控件--> 30 <EditText 31 android:id="@+id/EditTextuid" 32 android:singleLine="true" 33 android:layout_width="fill_parent" 34 android:layout_height="wrap_content" 35 android:layout_marginLeft="0dip" 36 android:text="e1001"> <!--設(shè)置文本--> 37 </EditText> 38 </LinearLayout> <!--LinearLayout--> 39 <LinearLayout 40 android:id="@+id/LinearLayout02" 41 android:orientation="horizontal" 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content"> <!--縱向自適應(yīng)大小--> 44 <TextView 45 android:text="密 碼:" 46 android:id="@+id/TextView03" 47 android:textColor="#222222" 48 android:layout_width="wrap_content" 49 android:layout_height="40dip" 50 android:layout_marginLeft="5dip" 51 android:textSize="18dip" 52 android:gravity="center_vertical"><!--設(shè)置位置--> 53 </TextView> <!--TextView控件--> 54 <EditText 55 android:id="@+id/EditTextPwd" 56 android:singleLine="true" 57 android:layout_width="fill_parent" 58 android:layout_height="wrap_content" 59 android:text="123456"> <!--設(shè)置文本--> 60 </EditText> 61 </LinearLayout> 62 <LinearLayout 63 android:id="@+id/LinearLayout03" 64 android:orientation="horizontal" 65 android:layout_width="wrap_content" 66 android:layout_height="wrap_content"> <!--縱向自適應(yīng)大小--> 67 <Button 68 android:text=" 登錄 " 69 android:id="@+id/loginLog" 70 android:layout_width="75dip" 71 android:layout_height="40dip" 72 android:textSize="18dip" 73 android:gravity="center"> <!--設(shè)置位置--> 74 </Button> <!--Button控件--> 75 <Button 76 android:text=" 清空 " 77 android:id="@+id/loginClear" 78 android:layout_width="75dip" 79 android:layout_height="40dip" 80 android:textSize="18dip" 81 android:gravity="center"> <!--設(shè)置位置--> 82 </Button> <!--Button控件--> 83 </LinearLayout> <!--LinearLayout--> 84 </LinearLayout> <!--LinearLayout--> 85 </LinearLayout> <!--LinearLayout-->
上面已經(jīng)介紹了本程序的主界面main.xml的開(kāi)發(fā),接下來(lái)將要為讀者介紹本程序的具體功能的實(shí)現(xiàn),代碼如下。
代碼位置:見(jiàn)隨書(shū)光盤(pán)中源代碼/第2章/Sample2_10/src/com/bn/ex2j目錄下的Sample2_10_Activity.class。
1 package com.bn.ex2j; //聲明包 2 ……//該處省略了部分類的導(dǎo)入,讀者可自行查看隨書(shū)光盤(pán)中的源代碼 3 import android.widget.Toast; //導(dǎo)入相關(guān)類 4 public class Sample2_10_Activity extends Activity { //繼承自Activity類 5 @Override 6 public void onCreate(Bundle savedInstanceState){ //重寫(xiě)的方法 7 super.onCreate(savedInstanceState); //調(diào)用父類 8 setContentView(R.layout.main); //跳轉(zhuǎn)到主界面 9 Button bLogin =(Button)this.findViewById(R.id.loginLog);//“登錄”按鈕 10 Button bClear=(Button)this.findViewById(R.id.loginClear); //“清空”按鈕 11 final EditText eUid=(EditText)this.findViewById(R.id.EditTextuid); //用戶名 12 final EditText eMima=(EditText)this.findViewById(R.id.EditTextPwd);//密碼 13 bLogin.setOnClickListener( //添加“登錄”按鈕監(jiān)聽(tīng)器 14 new OnClickListener(){ //匿名內(nèi)部類 15 @Override 16 public void onClick(View v){ //重寫(xiě)的方法 17 String strUid=eUid.getText().toString().trim(); //得到eUid的字符串 18 String strPwd=eMima.getText().toString().trim(); //得到eMima的字符串 19 if(strUid.equals("e1001")&&strPwd.equals("123456")){ //判斷是否符合條件 20 Toast.makeText(Sample2_10_Activity.this, //彈出Toast 21 "恭喜您登錄成功!", 22 Toast.LENGTH_SHORT).show(); 23 }else{ 24 Toast.makeText(Sample2_10_Activity.this, //彈出Toast 25 "請(qǐng)輸入正確的用戶名或密碼!", 26 Toast.LENGTH_SHORT).show(); 27 }}}); 28 bClear.setOnClickListener( //添加“清空”按鈕監(jiān)聽(tīng)器 29 new OnClickListener(){ //匿名內(nèi)部類 30 @Override 31 public void onClick(View v){ //重寫(xiě)的方法 32 eUid.setText(""); //設(shè)置用戶名為空 33 eMima.setText(""); //設(shè)置密碼為空 34 }});}}
其中:
● 第14~27行表示單擊“登錄”按鈕之后,判斷其用戶名和密碼是否符合條件。
● 第28~34行表示單擊“清空”按鈕之后,將顯示的用戶名和密碼全部清空。
- 元器件易學(xué)通:常用器件分冊(cè)
- 5G通信系統(tǒng)定位技術(shù)原理與方法
- 鴻蒙原生應(yīng)用開(kāi)發(fā):ArkTS語(yǔ)言快速上手
- 5G無(wú)線網(wǎng)絡(luò)及關(guān)鍵技術(shù)
- 交換機(jī)/路由器及其配置
- 液晶電視機(jī)檢修手冊(cè)
- 電子產(chǎn)品設(shè)計(jì)原理與應(yīng)用
- 走進(jìn)業(yè)余無(wú)線電
- 5G大規(guī)模天線增強(qiáng)技術(shù)
- 視頻精講:PADS 2007原理圖與布板設(shè)計(jì)典型實(shí)例
- 光波分復(fù)用系統(tǒng)與維護(hù)
- 光電子技術(shù)基礎(chǔ)與技能
- 光傳送網(wǎng)(OTN)技術(shù)、設(shè)備及工程應(yīng)用
- 輕松玩轉(zhuǎn)DSP:基于TMS320F2833x
- 新型手機(jī)原理與維修