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 }

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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);
// 监听文本框内容变化
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)) {
// 用户名为空,设置按钮不可见
bt_username_clear.setVisibility(View.INVISIBLE);
} else {
// 用户名不为空,设置按钮可见
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);
// 监听文本框内容变化
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)) {
// 用户名为空,设置按钮不可见
bt_pwd_clear.setVisibility(View.INVISIBLE);
} else {
// 用户名不为空,设置按钮可见
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;
}
// 默认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);
// 设置EditText的密码可见
password.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
} else {
// 密码不接见
bt_pwd_eye.setBackgroundResource(R.drawable.password_close);
// 设置EditText的密码隐藏
password.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}
}
posted @ 2018-03-09 15:13  ZzPink  阅读(411)  评论(0编辑  收藏  举报