Android 登陆功能实现
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:custom="http://schemas.android.com/apk/res-auto" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:fitsSystemWindows="true" > 6 <RelativeLayout 7 android:id="@+id/login_layout" 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:layout_marginLeft="20dp" 11 android:layout_marginRight="20dp" 12 android:gravity="center" > 13 <FrameLayout 14 android:id="@+id/username_layout" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:layout_marginTop="55dp" 18 android:gravity="center" > 19 <!-- android:inputType="number" --> 20 <EditText 21 android:id="@+id/username" 22 android:layout_width="fill_parent" 23 android:layout_height="40dp" 24 android:layout_marginTop="5dp" 25 android:maxLength="20" 26 android:paddingLeft="55dp" 27 android:paddingRight="60dp" > 28 </EditText> 29 <ImageView 30 android:layout_width="22dp" 31 android:layout_height="21dp" 32 android:layout_gravity="left|center_vertical" 33 android:layout_marginStart="10dp" 34 android:background="@drawable/username" 35 android:visibility="visible" /> 36 <TextView 37 android:id="@+id/contry_sn" 38 android:layout_width="40dp" 39 android:layout_height="50dp" 40 android:layout_gravity="left|center_vertical" 41 android:layout_marginTop="4dp" 42 android:gravity="center" 43 android:text="+62" 44 android:textColor="@android:color/black" 45 android:textSize="18sp" 46 android:visibility="invisible" /> 47 <Button 48 android:id="@+id/bt_username_clear" 49 android:layout_width="35dp" 50 android:layout_height="35dp" 51 android:layout_gravity="right|center_vertical" 52 android:layout_marginRight="10dp" 53 android:background="@drawable/email_delete_pressed" 54 android:visibility="invisible" /> 55 </FrameLayout> 56 <FrameLayout 57 android:id="@+id/usercode_layout" 58 android:layout_width="fill_parent" 59 android:layout_height="wrap_content" 60 android:layout_below="@id/username_layout" 61 android:layout_marginTop="6dp" 62 android:gravity="center" > 63 <EditText 64 android:id="@+id/password" 65 android:layout_width="fill_parent" 66 android:layout_height="40dp" 67 android:inputType="textPassword" 68 android:maxLength="20" 69 android:paddingLeft="55dp" 70 android:paddingRight="60dp" > 71 </EditText> 72 <ImageView 73 android:layout_width="18dp" 74 android:layout_height="21dp" 75 android:layout_gravity="left|center_vertical" 76 android:layout_marginStart="10dp" 77 android:background="@drawable/password" /> 78 <Button 79 android:id="@+id/bt_pwd_eye" 80 android:layout_width="40dp" 81 android:layout_height="40dp" 82 android:layout_gravity="right|center_vertical" 83 android:layout_marginRight="10dp" 84 android:background="@drawable/password_close" /> 85 <Button 86 android:id="@+id/bt_pwd_clear" 87 android:layout_width="35dp" 88 android:layout_height="35dp" 89 android:layout_gravity="right|center_vertical" 90 android:layout_marginRight="45dp" 91 android:background="@drawable/email_delete_pressed" 92 android:visibility="invisible" /> 93 </FrameLayout> 94 <Button 95 android:id="@+id/login" 96 android:layout_width="fill_parent" 97 android:layout_height="40dp" 98 android:layout_below="@id/usercode_layout" 99 android:layout_marginTop="30dp" 100 android:background="@drawable/login_selector" 101 android:gravity="center" 102 android:text="登录" 103 android:textColor="@android:color/white" /> 104 <Button 105 android:id="@+id/forgive_pwd" 106 android:layout_width="wrap_content" 107 android:layout_height="wrap_content" 108 android:layout_alignRight="@id/login" 109 android:layout_below="@id/login" 110 android:background="#00000000" 111 android:text="忘记密码?" 112 android:textColor="@drawable/text_color_selector" 113 android:textSize="16sp" /> 114 <Button 115 android:id="@+id/register" 116 android:layout_width="wrap_content" 117 android:layout_height="wrap_content" 118 android:layout_alignLeft="@id/login" 119 android:layout_below="@id/login" 120 android:background="#00000000" 121 android:gravity="left|center_vertical" 122 android:text="注册" 123 android:textColor="@drawable/text_color_selector" 124 android:textSize="16sp" 125 android:visibility="visible" /> 126 </RelativeLayout> 127 </RelativeLayout>
MainActivity如下:
1 package com.example.logindemo; 2 import android.support.v7.app.ActionBarActivity; 3 import android.text.Editable; 4 import android.text.TextWatcher; 5 import android.text.method.HideReturnsTransformationMethod; 6 import android.text.method.PasswordTransformationMethod; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.EditText; 11 import android.widget.Toast; 12 import android.os.Bundle; 13 /** 14 * 登录界面Demo 15 * 16 * @author ZHY 17 * 18 */ 19 public class MainActivity extends ActionBarActivity implements OnClickListener { 20 private EditText username, password; 21 private Button bt_username_clear; 22 private Button bt_pwd_clear; 23 private Button forgive_pwd; 24 private Button bt_pwd_eye; 25 private Button login; 26 private Button register; 27 private boolean isOpen = false; 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 initView(); 33 } 34 private void initView() { 35 username = (EditText) findViewById(R.id.username); 36 // 监听文本框内容变化 37 username.addTextChangedListener(new TextWatcher() { 38 @Override 39 public void onTextChanged(CharSequence s, int start, int before, 40 int count) { 41 // 获得文本框中的用户 42 String user = username.getText().toString().trim(); 43 if ("".equals(user)) { 44 // 用户名为空,设置按钮不可见 45 bt_username_clear.setVisibility(View.INVISIBLE); 46 } else { 47 // 用户名不为空,设置按钮可见 48 bt_username_clear.setVisibility(View.VISIBLE); 49 } 50 } 51 @Override 52 public void beforeTextChanged(CharSequence s, int start, int count, 53 int after) { 54 } 55 @Override 56 public void afterTextChanged(Editable s) { 57 } 58 }); 59 password = (EditText) findViewById(R.id.password); 60 // 监听文本框内容变化 61 password.addTextChangedListener(new TextWatcher() { 62 @Override 63 public void onTextChanged(CharSequence s, int start, int before, 64 int count) { 65 // 获得文本框中的用户 66 String pwd = password.getText().toString().trim(); 67 if ("".equals(pwd)) { 68 // 用户名为空,设置按钮不可见 69 bt_pwd_clear.setVisibility(View.INVISIBLE); 70 } else { 71 // 用户名不为空,设置按钮可见 72 bt_pwd_clear.setVisibility(View.VISIBLE); 73 } 74 } 75 @Override 76 public void beforeTextChanged(CharSequence s, int start, int count, 77 int after) { 78 } 79 @Override 80 public void afterTextChanged(Editable s) { 81 } 82 }); 83 bt_username_clear = (Button) findViewById(R.id.bt_username_clear); 84 bt_username_clear.setOnClickListener(this); 85 bt_pwd_clear = (Button) findViewById(R.id.bt_pwd_clear); 86 bt_pwd_clear.setOnClickListener(this); 87 88 bt_pwd_eye = (Button) findViewById(R.id.bt_pwd_eye); 89 bt_pwd_eye.setOnClickListener(this); 90 login = (Button) findViewById(R.id.login); 91 login.setOnClickListener(this); 92 egister = (Button) findViewById(R.id.register); 93 register.setOnClickListener(this); 94 forgive_pwd = (Button) findViewById(R.id.forgive_pwd); 95 forgive_pwd.setOnClickListener(this); 96 } 97 @Override 98 public void onClick(View v) { 99 switch (v.getId()) { 100 case R.id.bt_username_clear: 101 // 清除登录名 102 username.setText(""); 103 break; 104 case R.id.bt_pwd_clear: 105 // 清除密码 106 password.setText(""); 107 break; 108 case R.id.bt_pwd_eye: 109 // 密码可见与不可见的切换 110 if (isOpen) { 111 isOpen = false; 112 } else { 113 isOpen = true; 114 } 115 // 默认isOpen是false,密码不可见 116 changePwdOpenOrClose(isOpen); 117 break; 118 case R.id.login: 119 // TODO 登录按钮 120 break; 121 case R.id.register: 122 // 注册按钮 123 Toast.makeText(MainActivity.this, "注册", 0).show(); 124 break; 125 case R.id.forgive_pwd: 126 // 忘记密码按钮 127 Toast.makeText(MainActivity.this, "忘记密码", 0).show(); 128 break; 129 default: 130 break; 131 } 132 } 133 /** 134 * 密码可见与不可见的切换 135 * 136 * @param flag 137 */ 138 private void changePwdOpenOrClose(boolean flag) { 139 // 第一次过来是false,密码不可见 140 if (flag) { 141 // 密码可见 142 bt_pwd_eye.setBackgroundResource(R.drawable.password_open); 143 // 设置EditText的密码可见 144 password.setTransformationMethod(HideReturnsTransformationMethod 145 .getInstance()); 146 } else { 147 // 密码不接见 148 bt_pwd_eye.setBackgroundResource(R.drawable.password_close); 149 // 设置EditText的密码隐藏 150 password.setTransformationMethod(PasswordTransformationMethod 151 .getInstance()); 152 } 153 } 154 }
MainActivity如下: