今天分享一下登陸界面用戶登錄名的檢測(cè),大家都知道如果在服務(wù)器端進(jìn)行所有用戶名的檢測(cè)是比較浪費(fèi)資源的。用戶每點(diǎn)擊一次登陸就要發(fā)到服務(wù)端去檢測(cè),對(duì)于服務(wù)端來(lái)說(shuō)負(fù)荷是比較大的。所以呢在客服端對(duì)用戶的非法信息進(jìn)行簡(jiǎn)單的過(guò)濾是非常有必要的。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、雅安服務(wù)器托管、營(yíng)銷軟件、網(wǎng)站建設(shè)、花山網(wǎng)站維護(hù)、網(wǎng)站推廣。
源碼下載:Android用戶名檢測(cè)
首先看一下效果:
當(dāng)用戶輸入的用戶名長(zhǎng)度小于3,或者大于9時(shí)將出現(xiàn)紅色提示,并且登陸按鈕不可點(diǎn)擊。
當(dāng)輸入的用戶名大在合法區(qū)間則提示消失,如果密碼不為空則登陸按鈕可點(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" 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è)輸入的用戶名操作 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)行用戶名的檢測(cè) if(etUser.hasFocus()==false){ //如果用戶名長(zhǎng)度小于3或者大于9,則提示用戶名錯(cuò)誤且登陸不可點(diǎn)擊 if(etUser.getText().toString().length()>9||etUser.getText().toString().length()<3){ tvTip.setText("用戶名不合法!"); button.setClickable(false); }else{ //如果用戶名合法且密碼不為空,設(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ì)用戶名進(jìn)行判斷。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
新聞標(biāo)題:Android登陸界面用戶名檢測(cè)功能
當(dāng)前鏈接:http://www.rwnh.cn/article0/iggjio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、響應(yīng)式網(wǎng)站、電子商務(wù)、網(wǎng)站改版、品牌網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)