Android登錄界面的實(shí)現(xiàn)代碼分享
最近由于項(xiàng)目需要,寶寶好久沒搞Android啦,又是因?yàn)轫?xiàng)目需要,現(xiàn)在繼續(xù)弄Android,哎,說多了都是淚呀,別的不用多說,先搞一個登錄界面練練手,登錄界面可以說是Android項(xiàng)目中最常用也是最基本的,如果這個都搞不定,那可以直接去跳21世紀(jì)樓啦。
廢話不多說,先上效果圖了,如果大家感覺還不錯,請參考實(shí)現(xiàn)代碼吧。

相信這種渣渣布局對很多人來說太簡單啦,直接上布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fitsSystemWindows="true" > <RelativeLayout android:id="@+id/login_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:gravity="center" > <FrameLayout android:id="@+id/username_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="55dp" android:gravity="center" > <!-- android:inputType="number" --> <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginTop="5dp" android:maxLength="20" android:paddingLeft="55dp" android:paddingRight="60dp" > </EditText> <ImageView android:layout_width="22dp" android:layout_height="21dp" android:layout_gravity="left|center_vertical" android:layout_marginStart="10dp" android:background="@drawable/username" android:visibility="visible" /> <TextView android:id="@+id/contry_sn" android:layout_width="40dp" android:layout_height="50dp" android:layout_gravity="left|center_vertical" android:layout_marginTop="4dp" android:gravity="center" android:text="+62" android:textColor="@android:color/black" android:textSize="18sp" android:visibility="invisible" /> <Button android:id="@+id/bt_username_clear" android:layout_width="35dp" android:layout_height="35dp" android:layout_gravity="right|center_vertical" android:layout_marginRight="10dp" android:background="@drawable/email_delete_pressed" android:visibility="invisible" /> </FrameLayout> <FrameLayout android:id="@+id/usercode_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/username_layout" android:layout_marginTop="6dp" android:gravity="center" > <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="40dp" android:inputType="textPassword" android:maxLength="20" android:paddingLeft="55dp" android:paddingRight="60dp" > </EditText> <ImageView android:layout_width="18dp" android:layout_height="21dp" android:layout_gravity="left|center_vertical" android:layout_marginStart="10dp" android:background="@drawable/password" /> <Button android:id="@+id/bt_pwd_eye" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="right|center_vertical" android:layout_marginRight="10dp" android:background="@drawable/password_close" /> <Button android:id="@+id/bt_pwd_clear" android:layout_width="35dp" android:layout_height="35dp" android:layout_gravity="right|center_vertical" android:layout_marginRight="45dp" android:background="@drawable/email_delete_pressed" android:visibility="invisible" /> </FrameLayout> <Button android:id="@+id/login" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_below="@id/usercode_layout" android:layout_marginTop="30dp" android:background="@drawable/login_selector" android:gravity="center" android:text="登錄" android:textColor="@android:color/white" /> <Button android:id="@+id/forgive_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/login" android:layout_below="@id/login" android:background="#00000000" android:text="忘記密碼?" android:textColor="@drawable/text_color_selector" android:textSize="16sp" /> <Button android:id="@+id/register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/login" android:layout_below="@id/login" android:background="#00000000" android:gravity="left|center_vertical" android:text="注冊" android:textColor="@drawable/text_color_selector" android:textSize="16sp" android:visibility="visible" /> </RelativeLayout> </RelativeLayout>
MainActivity如下:
package com.example.logindemo;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Bundle;
/**
* 登錄界面Demo
*
* @author ZHY
*
*/
public class MainActivity extends ActionBarActivity implements OnClickListener {
private EditText username, password;
private Button bt_username_clear;
private Button bt_pwd_clear;
private Button forgive_pwd;
private Button bt_pwd_eye;
private Button login;
private Button register;
private boolean isOpen = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
username = (EditText) findViewById(R.id.username);
// 監(jiān)聽文本框內(nèi)容變化
username.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// 獲得文本框中的用戶
String user = username.getText().toString().trim();
if ("".equals(user)) {
// 用戶名為空,設(shè)置按鈕不可見
bt_username_clear.setVisibility(View.INVISIBLE);
} else {
// 用戶名不為空,設(shè)置按鈕可見
bt_username_clear.setVisibility(View.VISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
password = (EditText) findViewById(R.id.password);
// 監(jiān)聽文本框內(nèi)容變化
password.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// 獲得文本框中的用戶
String pwd = password.getText().toString().trim();
if ("".equals(pwd)) {
// 用戶名為空,設(shè)置按鈕不可見
bt_pwd_clear.setVisibility(View.INVISIBLE);
} else {
// 用戶名不為空,設(shè)置按鈕可見
bt_pwd_clear.setVisibility(View.VISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
bt_username_clear = (Button) findViewById(R.id.bt_username_clear);
bt_username_clear.setOnClickListener(this);
bt_pwd_clear = (Button) findViewById(R.id.bt_pwd_clear);
bt_pwd_clear.setOnClickListener(this);
bt_pwd_eye = (Button) findViewById(R.id.bt_pwd_eye);
bt_pwd_eye.setOnClickListener(this);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(this);
egister = (Button) findViewById(R.id.register);
register.setOnClickListener(this);
forgive_pwd = (Button) findViewById(R.id.forgive_pwd);
forgive_pwd.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_username_clear:
// 清除登錄名
username.setText("");
break;
case R.id.bt_pwd_clear:
// 清除密碼
password.setText("");
break;
case R.id.bt_pwd_eye:
// 密碼可見與不可見的切換
if (isOpen) {
isOpen = false;
} else {
isOpen = true;
}
// 默認(rèn)isOpen是false,密碼不可見
changePwdOpenOrClose(isOpen);
break;
case R.id.login:
// TODO 登錄按鈕
break;
case R.id.register:
// 注冊按鈕
Toast.makeText(MainActivity.this, "注冊", 0).show();
break;
case R.id.forgive_pwd:
// 忘記密碼按鈕
Toast.makeText(MainActivity.this, "忘記密碼", 0).show();
break;
default:
break;
}
}
/**
* 密碼可見與不可見的切換
*
* @param flag
*/
private void changePwdOpenOrClose(boolean flag) {
// 第一次過來是false,密碼不可見
if (flag) {
// 密碼可見
bt_pwd_eye.setBackgroundResource(R.drawable.password_open);
// 設(shè)置EditText的密碼可見
password.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
} else {
// 密碼不接見
bt_pwd_eye.setBackgroundResource(R.drawable.password_close);
// 設(shè)置EditText的密碼隱藏
password.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}
}
}
Ok,就是這么簡單,效果完成。
以上所述是小編給大家介紹的Android登錄界面的實(shí)現(xiàn)代碼分享,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android?Studio實(shí)現(xiàn)登錄界面功能
- Android實(shí)現(xiàn)登錄界面的注冊功能
- Android實(shí)現(xiàn)注冊登錄界面的實(shí)例代碼
- Android設(shè)計(jì)登錄界面、找回密碼、注冊功能
- Android實(shí)現(xiàn)簡潔的APP登錄界面
- 功能強(qiáng)大的登錄界面Android實(shí)現(xiàn)代碼
- Android屬性動畫實(shí)現(xiàn)炫酷的登錄界面
- Android開發(fā)實(shí)例之登錄界面的實(shí)現(xiàn)
- Android QQ登錄界面繪制代碼
- Android Studio實(shí)現(xiàn)簡易登錄界面制作
相關(guān)文章
ActivityManagerService之Service啟動過程解析
這篇文章主要為大家介紹了ActivityManagerService之Service啟動過程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Ubantu16.04進(jìn)行Android 8.0源碼編譯的流程
這篇文章主要介紹了Ubantu16.04進(jìn)行Android 8.0源碼編譯的相關(guān)資料,需要的朋友可以參考下2018-02-02
android實(shí)現(xiàn)緩存圖片等數(shù)據(jù)
本文給大家分享的是Android采用LinkedHashMap自帶的LRU 算法緩存數(shù)據(jù)的方法和示例,有需要的小伙伴可以參考下。2015-07-07
Android實(shí)現(xiàn)TextView字符串關(guān)鍵字變色的方法
這篇文章顯示給大家介紹了字符串中關(guān)鍵字變色的實(shí)現(xiàn)方法,而后又拓展介紹了在Android中如何實(shí)現(xiàn)搜索關(guān)鍵字變色,相信對各位Android開發(fā)者們具有一定的參考借鑒價值,感興趣的朋友們下面來一起看看吧。2016-10-10
浮動AppBar中的textField焦點(diǎn)回滾問題解決
這篇文章主要為大家介紹了浮動AppBar中的textField焦點(diǎn)回滾問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android實(shí)現(xiàn)朋友圈評論回復(fù)列表
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)朋友圈評論回復(fù)列表,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
淺談Android硬件加速原理與實(shí)現(xiàn)簡介
這篇文章主要介紹了淺談Android硬件加速原理與實(shí)現(xiàn)簡介,本文嘗試從底層硬件原理,一直到上層代碼實(shí)現(xiàn),對硬件加速技術(shù)進(jìn)行簡單介紹,感興趣的小伙伴們可以參考一下2018-07-07

