概述
今天是我第一次写技术博客,本人也是小菜鸟一枚,工作不满一年,博客内容也比较简单,对专业知识了解不够深入,写博客的原因一来是为了分享,而来也是激励自己,如果内容有什么错误问题,请大家指教纠正。
今天要讲的是六位密码输入框。先上代码。首先是layout.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:gravity="center"
android:layout_gravity="center"
android:orientation="horizontal">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/e1"
android:layout_width="45dip"
android:layout_height="fill_parent"
android:background="@drawable/topayl"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1" />
<TextView
android:id="@+id/heng1"
android:layout_width="15dip"
android:layout_height="2dip"
android:layout_gravity="center"
android:background="@color/black"
android:gravity="center"
android:visibility="gone" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/e2"
android:layout_width="45dip"
android:layout_height="fill_parent"
android:background="@drawable/topaym"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1" />
<TextView
android:id="@+id/heng2"
android:layout_width="15dip"
android:layout_height="2dip"
android:layout_gravity="center"
android:background="@color/black"
android:gravity="center"
android:visibility="gone" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/e3"
android:layout_width="45dip"
android:layout_height="fill_parent"
android:background="@drawable/topaym"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1" />
<TextView
android:id="@+id/heng3"
android:layout_width="15dip"
android:layout_height="2dip"
android:layout_gravity="center"
android:background="@color/black"
android:gravity="center"
android:visibility="gone" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/e4"
android:layout_width="45dip"
android:layout_height="fill_parent"
android:background="@drawable/topaym"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1" />
<TextView
android:id="@+id/heng4"
android:layout_width="15dip"
android:layout_height="2dip"
android:layout_gravity="center"
android:background="@color/black"
android:gravity="center"
android:visibility="gone" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/e5"
android:layout_width="45dip"
android:layout_height="fill_parent"
android:background="@drawable/topaym"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1" />
<TextView
android:id="@+id/heng5"
android:layout_width="15dip"
android:layout_height="2dip"
android:layout_gravity="center"
android:background="@color/black"
android:gravity="center"
android:visibility="gone" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/e6"
android:layout_width="45dip"
android:layout_height="fill_parent"
android:background="@drawable/topayr"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1" />
<TextView
android:id="@+id/heng6"
android:layout_width="15dip"
android:layout_height="2dip"
android:layout_gravity="center"
android:background="@color/black"
android:gravity="center"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
以上代码比较简单,这里六个EditText下面的TextView暂时不看。出来的效果是六个框框。
下面是自定义View的代码
/**
* Created by HYQ on 2016/3/3.
*/
public class SixPwdView extends LinearLayout{
private Context context;
private SixPwdListener listener;
private EditText e1,e2,e3,e4,e5,e6;
private String s_e1, s_e2, s_e3, s_e4, s_e5, s_e6;
private onKeyListeners onkeylistener;
public SixPwdView(Context context) {
super(context);
this.context = context;
}
public SixPwdView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initView();
}
public void clear_edit() {
e1.setText("");
e2.setText("");
e3.setText("");
e4.setText("");
e5.setText("");
e6.setText("");
}
/**
* 取消e6的焦点
*/
public void clearLastFouse(){
e6.setFocusable(false);
e6.setFocusableInTouchMode(false);
e6.clearFocus();
}
public void getLastFouse(){
e6.setFocusable(true);
e6.setFocusableInTouchMode(true);
e6.requestFocus();
e6.findFocus();
}
//初始化view
private void initView() {
View view = LayoutInflater.from(context).inflate(R.layout.six_pwd_layout,this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.height = (int) context.getResources().getDimension(R.dimen.dp52);
view.setLayoutParams(params);
e1 = (EditText)view.findViewById(R.id.e1);
e2 = (EditText)view.findViewById(R.id.e2);
e3 = (EditText)view.findViewById(R.id.e3);
e4 = (EditText)view.findViewById(R.id.e4);
e5 = (EditText)view.findViewById(R.id.e5);
e6 = (EditText)view.findViewById(R.id.e6);
onkeylistener = new onKeyListeners();
e1.setOnKeyListener(onkeylistener);
e2.setOnKeyListener(onkeylistener);
e3.setOnKeyListener(onkeylistener);
e4.setOnKeyListener(onkeylistener);
e5.setOnKeyListener(onkeylistener);
e6.setOnKeyListener(onkeylistener);
e1.setCursorVisible(false);
e2.setCursorVisible(false);
e3.setCursorVisible(false);
e4.setCursorVisible(false);
e5.setCursorVisible(false);
e6.setCursorVisible(false);
clear_focuse();
setaddTextChangedListener();
}
private void setaddTextChangedListener(){
e1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (e1.getText().toString().equals("")) {
// heng1.setVisibility(View.VISIBLE);
} else {
s_e1 = e1.getText().toString();
e2_focuse();
}
}
});
e2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (e2.getText().toString().equals("")) {
} else {
s_e2 = e2.getText().toString();
e3_focuse();
}
}
});
e3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (e3.getText().toString().equals("")) {
} else {
s_e3 = e3.getText().toString();
e4_focuse();
}
}
});
e4.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (e4.getText().toString().equals("")) {
} else {
s_e4 = e4.getText().toString();
e5_focuse();
}
}
});
e5.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (e5.getText().toString().equals("")) {
} else {
s_e5 = e5.getText().toString();
last_focuse();
}
}
});
e6.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (e6.getText().toString().equals("")) {
} else {
e6.setFocusable(false);
s_e6 = e6.getText().toString();
isfirstallwrite();
}
}
});
}
public void setLastFocuse(){
e6.setFocusable(true);
e6.setFocusableInTouchMode(true);
e6.requestFocus();
e6.findFocus();
}
public void getLockTime(){
e1.setFocusable(false);
e2.setFocusable(false);
e3.setFocusable(false);
e4.setFocusable(false);
e5.setFocusable(false);
e6.setFocusable(false);
}
String eString = "";
private void isfirstallwrite() {
if (!TextUtils.isEmpty(s_e1) && !TextUtils.isEmpty(s_e2) && !TextUtils.isEmpty(s_e3) && !TextUtils.isEmpty(s_e4)
&& !TextUtils.isEmpty(s_e5) && !TextUtils.isEmpty(s_e6)) {
//getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
eString = s_e1 + s_e2 + s_e3 + s_e4 + s_e5 + s_e6;
if (listener != null) {
listener.onInputComplete(eString);
}
}
}
/**
* 点击删除按钮监听
*
* @author
*
*/
class onKeyListeners implements android.view.View.OnKeyListener {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (event.getAction() != KeyEvent.ACTION_UP) {
return true;
}
if (e6.isFocused()) {
if (!TextUtils.isEmpty(e6.getText())) {
e6.setText("");
} else {
e6.clearFocus();
e5_focuse();
e5.setText("");
}
} else if (e5.isFocused()) {
if (!TextUtils.isEmpty(e5.getText())) {
e5.setText("");
} else {
e5.clearFocus();
e4_focuse();
e4.setText("");
}
} else if (e4.isFocused()) {
if (!TextUtils.isEmpty(e4.getText())) {
e4.setText("");
} else {
e4.clearFocus();
e3_focuse();
e3.setText("");
}
} else if (e3.isFocused()) {
if (!TextUtils.isEmpty(e3.getText())) {
e3.setText("");
} else {
e3.clearFocus();
e2_focuse();
e2.setText("");
}
} else if (e2.isFocused()) {
if (!TextUtils.isEmpty(e2.getText())) {
e2.setText("");
} else {
e2.clearFocus();
clear_focuse();
e1.setText("");
}
}
return true;
}
return false;
}
}
private void e2_focuse() {
e2.setFocusable(true);
e2.setFocusableInTouchMode(true);
e2.requestFocus();
e2.findFocus();
e1.setFocusable(false);
e3.setFocusable(false);
e4.setFocusable(false);
e5.setFocusable(false);
e6.setFocusable(false);
}
public void clear_focuse() {
e1.setFocusable(true);
e1.setFocusableInTouchMode(true);
e1.requestFocus();
e1.findFocus();
InputMethodManager inputManager = (InputMethodManager) e1.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(e1, 0);
e2.setFocusable(false);
e3.setFocusable(false);
e4.setFocusable(false);
e5.setFocusable(false);
e6.setFocusable(false);
}
private void e3_focuse() {
e3.setFocusable(true);
e3.setFocusableInTouchMode(true);
e3.requestFocus();
e3.findFocus();
e2.setFocusable(false);
e1.setFocusable(false);
e4.setFocusable(false);
e5.setFocusable(false);
e6.setFocusable(false);
}
private void e4_focuse() {
e4.setFocusable(true);
e4.setFocusableInTouchMode(true);
e4.requestFocus();
e4.findFocus();
e2.setFocusable(false);
e3.setFocusable(false);
e1.setFocusable(false);
e5.setFocusable(false);
e6.setFocusable(false);
}
private void e5_focuse() {
e5.setFocusable(true);
e5.setFocusableInTouchMode(true);
e5.requestFocus();
e5.findFocus();
e2.setFocusable(false);
e3.setFocusable(false);
e4.setFocusable(false);
e1.setFocusable(false);
e6.setFocusable(false);
}
public void last_focuse() {
e6.setFocusable(true);
e6.setFocusableInTouchMode(true);
e6.requestFocus();
e6.findFocus();
e2.setFocusable(false);
e3.setFocusable(false);
e4.setFocusable(false);
e5.setFocusable(false);
e1.setFocusable(false);
}
public void setListener(SixPwdListener listener) {
this.listener = listener;
}
public interface SixPwdListener{
void onInputComplete(String passWord);
}
}
很显然,在这个View里,首先setaddTextChangedListener()这个方法控制view的焦点,当e1中有数据值,立马剥夺焦点让给e2,当e2有数据值时,立马剥夺焦点让给e3,如此层层递进,直到e6,当e6有数据时,通过isfirstallwrite()这个方法收集到6个EditText里的值,也就是6位密码,然后通过接口SixPwdListener传出去,至于要对六位密码做什么处理,可以自行定义。这里要注意的是,当e6获取到值时,我这里做的处理是直接把e6的焦点设置为false,即e6.setFocusable(false),这里有一个问题,我这里自定义六位密码框,在前五个EditText时,按软键盘的删除键框内的数据是可以清楚的,只有当e6输入完成时不可以清楚,应为我的需求是输入到第六位完成后直接有跳转处理,所以当碰到输入完六位密码想删除更改时,也就是在 e6.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (e6.getText().toString().equals("")) {
} else {
e6.setFocusable(false);
s_e6 = e6.getText().toString();
isfirstallwrite();
}
}
});
}
这里afterTextChanged(Editable s) 里,各位可以使得e6有焦点,改成
@Override
public void afterTextChanged(Editable s) {
if (e6.getText().toString().equals("")) {
} else {
e6.setFocusable(false);
s_e6 = e6.getText().toString();
setLastFocuse();
isfirstallwrite(); } }
其中的setLastFocuse()方法如下:
public void setLastFocuse(){
e6.setFocusable(true);
e6.setFocusableInTouchMode(true);
e6.requestFocus();
e6.findFocus();
}
其实这个自定义View总体来说比较简单,有点基础的一看就懂,这里还要说的是,如果在editText输入完数据后,不剥夺焦点的话,他会有一段过度时间才会让数字变成圆点。剥夺的焦点的话会立马变,所以在e6处先剥夺焦点后在设置setFocusable为true.不知道我这么说大家听得懂不。。。呜呜呜,文笔不好咩.
最后
以上就是爱笑电灯胆为你收集整理的Android自定义View之六位密码框的全部内容,希望文章能够帮你解决Android自定义View之六位密码框所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复