自定義密碼框 讓你實現一格一格輸密碼
阿新 • • 發佈:2019-01-24
我自己手頭一直用的IOS系統,寫這個密碼框的時候功能方面也參考了一個下蘋果的螢幕解鎖。這個密碼框也是修修改改兩三天才算完善,效果雖然實現了,但很多細節地方還是不太明白,還希望高人指點。
1.功能介紹及效果圖
做密碼框的時候考慮了兩個方案,一個方案是用6個EditText,另一個方案則是在EditText上畫5條線。因為我基本沒接觸過draw部分的程式碼,對語法都不瞭解,所以果斷選擇了第一種。接下來考慮密碼框需要實現的功能和使用者體驗的問題。
功能1:輸入密碼刪除密碼流暢
功能2:使用者點選任意密碼框焦點在正確的位置
功能3:輸入的密碼直接顯示為圓點
2.原始碼
[java] view plain- package com.example.passwordview;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.os.Handler;
- import android.text.Editable;
- import android.text.TextUtils;
- import android.text.TextWatcher;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnKeyListener;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- public class MainActivity extends Activity implements TextWatcher{
- private EditText et_pwd1;
- private EditText et_pwd2;
- private EditText et_pwd3;
- private EditText et_pwd4;
- private EditText et_pwd5;
- private EditText et_pwd6;
- private List<EditText> et_group;
- private OnKeyListener onKeyListener;
- public int cursorPosition=0;
- private InputMethodManager imm;
- public Context context;
- private String[] password=new String[6];
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.httfund_enter_password);
- intiView();
- setListener();
- testcursorPosition();
- }
- public void intiView(){
- et_pwd1=(EditText)findViewById(R.id.et_pwd1);
- et_pwd2=(EditText)findViewById(R.id.et_pwd2);
- et_pwd3=(EditText)findViewById(R.id.et_pwd3);
- et_pwd4=(EditText)findViewById(R.id.et_pwd4);
- et_pwd5=(EditText)findViewById(R.id.et_pwd5);
- et_pwd6=(EditText)findViewById(R.id.et_pwd6);
- et_group=new ArrayList<EditText>();
- et_group.add(et_pwd1);
- et_group.add(et_pwd2);
- et_group.add(et_pwd3);
- et_group.add(et_pwd4);
- et_group.add(et_pwd5);
- et_group.add(et_pwd6);
- }
- /**
- * 輸入判斷,游標永遠在第一個空白格
- */
- public void testcursorPosition(){
- cursorPosition=0;
- for(EditText editView : et_group){
- editView.setFocusableInTouchMode(true);
- }
- for(EditText et : et_group){
- if(!TextUtils.isEmpty(et.getText().toString())){
- ++cursorPosition;
- }else{
- break;
- }
- }
- if(cursorPosition==6){
- cursorPosition=5;
- }
- EditText et=et_group.get(cursorPosition);
- et.requestFocus();
- et.setSelection(et.getText().toString().length());
- imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
- if(imm.isActive()){
- imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
- }
- setEditViewEnable();
- }
- /**
- * 設定EditView的Enable屬性
- * 僅將包含游標的EditView設定為true
- */
- public void setEditViewEnable(){
- int i=0;
- for(EditText et : et_group){
- if(i++!=cursorPosition){
- et.setFocusableInTouchMode(false);
- }
- }
- }
- /**
- * 事件監聽
- */
- public void setListener(){
- /**
- * EditView 監聽軟鍵盤,監聽刪除鍵
- */
- onKeyListener=new OnKeyListener() {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- // TODO Auto-generated method stub
- if(keyCode==KeyEvent.KEYCODE_DEL&&event.getAction()==KeyEvent.ACTION_DOWN){
- testcursorPosition();
- Log.d("pwd", "刪除密碼"+Integer.toString(cursorPosition));
- if(cursorPosition!=6&&cursorPosition!=0){
- EditText et=et_group.get(cursorPosition);
- if(TextUtils.isEmpty(et.getText().toString())){
- et_group.get(cursorPosition-1).setText("");
- }
- }
- }
- return false;
- }
- };
- /**
- * EditView的點選事件
- */
- OnClickListener clickListener=new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- testcursorPosition();
- }
- };
- for(EditText et : et_group){
- et.addTextChangedListener(this);
- et.setOnKeyListener(onKeyListener);
- et.setOnClickListener(clickListener);
- }
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- // TODO Auto-generated method stub
- EditText et = et_group.get(cursorPosition);
- if(s.length()>1){
- s=s.toString().subSequence(0, 1);
- et.setText(s);
- et.setSelection(s.length());
- }
- if(!TextUtils.isEmpty(et.getText().toString())&&
- !et.getText().toString().equals("*")){
- password[cursorPosition]=et.getText().toString();
- et.setText("*");
- }else if(!et.getText().toString().equals("*")){
- password[cursorPosition]=et.getText().toString();
- }
- testcursorPosition();
- }
- @Override
- public void afterTextChanged(Editable s) {
- // TODO Auto-generated method stub
- }
- }
package com.example.passwordview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements TextWatcher{
private EditText et_pwd1;
private EditText et_pwd2;
private EditText et_pwd3;
private EditText et_pwd4;
private EditText et_pwd5;
private EditText et_pwd6;
private List<EditText> et_group;
private OnKeyListener onKeyListener;
public int cursorPosition=0;
private InputMethodManager imm;
public Context context;
private String[] password=new String[6];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.httfund_enter_password);
intiView();
setListener();
testcursorPosition();
}
public void intiView(){
et_pwd1=(EditText)findViewById(R.id.et_pwd1);
et_pwd2=(EditText)findViewById(R.id.et_pwd2);
et_pwd3=(EditText)findViewById(R.id.et_pwd3);
et_pwd4=(EditText)findViewById(R.id.et_pwd4);
et_pwd5=(EditText)findViewById(R.id.et_pwd5);
et_pwd6=(EditText)findViewById(R.id.et_pwd6);
et_group=new ArrayList<EditText>();
et_group.add(et_pwd1);
et_group.add(et_pwd2);
et_group.add(et_pwd3);
et_group.add(et_pwd4);
et_group.add(et_pwd5);
et_group.add(et_pwd6);
}
/**
* 輸入判斷,游標永遠在第一個空白格
*/
public void testcursorPosition(){
cursorPosition=0;
for(EditText editView : et_group){
editView.setFocusableInTouchMode(true);
}
for(EditText et : et_group){
if(!TextUtils.isEmpty(et.getText().toString())){
++cursorPosition;
}else{
break;
}
}
if(cursorPosition==6){
cursorPosition=5;
}
EditText et=et_group.get(cursorPosition);
et.requestFocus();
et.setSelection(et.getText().toString().length());
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive()){
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
setEditViewEnable();
}
/**
* 設定EditView的Enable屬性
* 僅將包含游標的EditView設定為true
*/
public void setEditViewEnable(){
int i=0;
for(EditText et : et_group){
if(i++!=cursorPosition){
et.setFocusableInTouchMode(false);
}
}
}
/**
* 事件監聽
*/
public void setListener(){
/**
* EditView 監聽軟鍵盤,監聽刪除鍵
*/
onKeyListener=new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_DEL&&event.getAction()==KeyEvent.ACTION_DOWN){
testcursorPosition();
Log.d("pwd", "刪除密碼"+Integer.toString(cursorPosition));
if(cursorPosition!=6&&cursorPosition!=0){
EditText et=et_group.get(cursorPosition);
if(TextUtils.isEmpty(et.getText().toString())){
et_group.get(cursorPosition-1).setText("");
}
}
}
return false;
}
};
/**
* EditView的點選事件
*/
OnClickListener clickListener=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
testcursorPosition();
}
};
for(EditText et : et_group){
et.addTextChangedListener(this);
et.setOnKeyListener(onKeyListener);
et.setOnClickListener(clickListener);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
EditText et = et_group.get(cursorPosition);
if(s.length()>1){
s=s.toString().subSequence(0, 1);
et.setText(s);
et.setSelection(s.length());
}
if(!TextUtils.isEmpty(et.getText().toString())&&
!et.getText().toString().equals("*")){
password[cursorPosition]=et.getText().toString();
et.setText("*");
}else if(!et.getText().toString().equals("*")){
password[cursorPosition]=et.getText().toString();
}
testcursorPosition();
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
}
程式碼並不難,testcursorPosition用來檢測游標應該在的正確位置。InputMethodManager用於顯示軟鍵盤,設定成HIDE_IMPLICIT_ONLY唯一的一個問題就是可能彈出密碼框的時候會自動彈出軟鍵盤,這個軟鍵盤在彈出後會彈回,但是解決了在輸入密碼的時候軟鍵盤彈出彈回影響使用者體驗的問題。在onTextChanged對EditText的長度進行了限制。把數字顯示成“*”
整個邏輯跑通之後最大的問題就是輸入密碼變圓點,如果不直接設定成*號,密碼框變成圓點的過程會很長,而且時間不定,體驗非常差。
下載地址 :http://download.csdn.net/detail/u013107635/8026475