Android登陸界面用戶(hù)名檢測(cè)功能
今天分享一下登陸界面用戶(hù)登錄名的檢測(cè),大家都知道如果在服務(wù)器端進(jìn)行所有用戶(hù)名的檢測(cè)是比較浪費(fèi)資源的。用戶(hù)每點(diǎn)擊一次登陸就要發(fā)到服務(wù)端去檢測(cè),對(duì)于服務(wù)端來(lái)說(shuō)負(fù)荷是比較大的。所以呢在客服端對(duì)用戶(hù)的非法信息進(jìn)行簡(jiǎn)單的過(guò)濾是非常有必要的。
源碼下載:Android用戶(hù)名檢測(cè)
首先看一下效果:
當(dāng)用戶(hù)輸入的用戶(hù)名長(zhǎng)度小于3,或者大于9時(shí)將出現(xiàn)紅色提示,并且登陸按鈕不可點(diǎn)擊。
當(dāng)輸入的用戶(hù)名大在合法區(qū)間則提示消失,如果密碼不為空則登陸按鈕可點(diǎn)擊
雖然功能很小卻用到了不少的東西:
- EditText失去焦點(diǎn)事件的監(jiān)聽(tīng)
- 獲取輸入的字符并且檢測(cè)長(zhǎng)度
- 當(dāng)用戶(hù)名不合法時(shí)出現(xiàn)提示
- 設(shè)置登錄按鈕的不可點(diǎn)擊
接下來(lái)看一下源碼,為了是登陸界面更加美觀,我對(duì)登陸控件進(jìn)行了圓形化處理,也就是開(kāi)源醒目CircleImageView 項(xiàng)目主頁(yè)地址:https://github.com/hdodenhof/CircleImageView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/colorLogin" > <!-- Login progress --> <ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:visibility="gone" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="180dp" android:id="@+id/head_img" > <de.hdodenhof.circleimageview.CircleImageView android:layout_width="80dp" android:layout_height="80dp" android:src="@mipmap/nav_head" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="25dp" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="20dp" android:orientation="vertical"> <EditText android:id="@+id/et_user" android:layout_width="match_parent" android:layout_height="60dp" android:hint="@string/userName" android:background="@color/colorLoginForm" android:layout_marginBottom="5dp" /> <TextView android:id="@+id/tv_tip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/error" /> <EditText android:id="@+id/et_pass" android:layout_width="match_parent" android:layout_height="60dp" android:background="@color/colorLoginForm" android:hint="@string/passWord" android:paddingTop="1dp" /> </LinearLayout> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/loginButton" android:text="@string/loginButton" android:textColor="@color/colorLoginForm" /> </LinearLayout>
然后修改MainAvtivity.class:
public class MainActivity extends AppCompatActivity { EditText etUser; EditText etPassWord; TextView tvTip; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化View控件 findView(); //用于檢測(cè)輸入的用戶(hù)名操作 checkLength(); } private void checkLength() { //為etUser設(shè)置焦點(diǎn)改變監(jiān)聽(tīng)事件 etUser.setOnFocusChangeListener(new View.OnFocusChangeListener(){ @Override public void onFocusChange(View v, boolean hasFocus) { //如果失去焦點(diǎn)則進(jìn)行用戶(hù)名的檢測(cè) if(etUser.hasFocus()==false){ //如果用戶(hù)名長(zhǎng)度小于3或者大于9,則提示用戶(hù)名錯(cuò)誤且登陸不可點(diǎn)擊 if(etUser.getText().toString().length()>9||etUser.getText().toString().length()<3){ tvTip.setText("用戶(hù)名不合法!"); button.setClickable(false); }else{ //如果用戶(hù)名合法且密碼不為空,設(shè)置提示字體消失按鈕可點(diǎn)擊 if(etPassWord.getText().toString()!=""){ button.setClickable(true); tvTip.setText(""); } } } } }); } private void findView() { etUser= (EditText) findViewById(R.id.et_user); etPassWord= (EditText) findViewById(R.id.et_pass); tvTip= (TextView) findViewById(R.id.tv_tip); button= (Button) findViewById(R.id.button); } }
整個(gè)代碼的核心是編輯框的焦點(diǎn)改變的監(jiān)聽(tīng),然后對(duì)用戶(hù)名進(jìn)行判斷。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開(kāi)發(fā)中實(shí)現(xiàn)用戶(hù)注冊(cè)和登陸的代碼實(shí)例分享
- Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面
- Android如何通過(guò)手機(jī)獲取驗(yàn)證碼來(lái)完成注冊(cè)功能
- Android設(shè)計(jì)登錄界面、找回密碼、注冊(cè)功能
- Android登錄注冊(cè)功能 數(shù)據(jù)庫(kù)SQLite驗(yàn)證
- Android實(shí)現(xiàn)登錄注冊(cè)功能封裝
- Android實(shí)現(xiàn)簡(jiǎn)易登陸注冊(cè)邏輯的實(shí)例代碼
相關(guān)文章
Android簡(jiǎn)單實(shí)現(xiàn)啟動(dòng)畫(huà)面的方法
這篇文章主要介紹了Android簡(jiǎn)單實(shí)現(xiàn)啟動(dòng)畫(huà)面的方法,結(jié)合實(shí)例形式分析了啟動(dòng)畫(huà)面核心代碼及相關(guān)函數(shù),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android TextView的TextWatcher使用案例詳解
這篇文章主要介紹了Android TextView的TextWatcher使用案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android客戶(hù)端實(shí)現(xiàn)注冊(cè)、登錄詳解(1)
這篇文章主要為大家詳細(xì)介紹了Android客戶(hù)端實(shí)現(xiàn)注冊(cè)、登錄代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android中自定義view實(shí)現(xiàn)側(cè)滑效果
這篇文章主要介紹了Android中自定義view實(shí)現(xiàn)側(cè)滑效果的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11Android提高之使用NDK把彩圖轉(zhuǎn)換灰度圖的方法
這篇文章主要介紹了Android使用NDK把彩圖轉(zhuǎn)換灰度圖的方法,在Android項(xiàng)目開(kāi)發(fā)中有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08android實(shí)現(xiàn)Uri獲取真實(shí)路徑轉(zhuǎn)換成File的方法
這篇文章主要介紹了android實(shí)現(xiàn)Uri獲取真實(shí)路徑轉(zhuǎn)換成File的方法,涉及Android操作路徑的相關(guān)技巧,需要的朋友可以參考下2015-05-05Android 使用Zbar實(shí)現(xiàn)掃一掃功能
這篇文章主要介紹了Android 使用Zbar實(shí)現(xiàn)掃一掃功能,本文用的是Zbar實(shí)現(xiàn)掃一掃,因?yàn)楦鶕?jù)本人對(duì)兩個(gè)庫(kù)的使用比較,發(fā)現(xiàn)Zbar解碼比Zxing速度要快,實(shí)現(xiàn)方式也簡(jiǎn)單,需要的朋友可以參考下2023-03-03Android內(nèi)嵌Unity并實(shí)現(xiàn)互相跳轉(zhuǎn)的實(shí)例代碼
這篇文章主要介紹了Android內(nèi)嵌Unity并實(shí)現(xiàn)互相跳轉(zhuǎn)的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Android自定義View實(shí)現(xiàn)圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓形進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10