先上效果图:
实现思路: 1.四个TextView给textview一个下边框,.用一个透明的EditText覆盖在四个TextView上; 2.监听Textview 文本变化,获取输入内容,将值赋值给textview 3.四个textview输入完成后,添加输入完成回调,关闭软键盘;也可以监听删除回调
代码:
package com.app.captchademo.widget; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.View; import android.widget.EditText; import android.widget.RelativeLayout; import android.widget.TextView; import com.app.captchademo.R; /** * Created by shanshan on 2017/3/31. */ /** * 验证码页是四个textview。 * 思路: * 1.四个TextView给textview一个下边框,.用一个透明的EditText覆盖在四个TextView上; * 2.监听Textview 文本变化,获取输入内容,将值赋值给textview * 3.四个textview输入完成后,添加输入完成回调,关闭软键盘;也可以监听删除回调 */ public class VerificationCodeView extends RelativeLayout { private TextView[] textViews; private Context mcontext; private EditText editText; private StringBuffer stringBuffer = new StringBuffer(); private int count = 4; //验证码个数 private String inputContent; public VerificationCodeView(Context context) { this(context, null); mcontext = context; } public VerificationCodeView(Context context, AttributeSet attrs) { this(context, attrs, 0); mcontext = context; } public VerificationCodeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mcontext = context; //创建数组,放textview textViews = new TextView[4]; //验证码布局 初始化 View.inflate(context, R.layout.view_verification_code, this); editText = (EditText) findViewById(R.id.et_edittext); textViews[0] = (TextView) findViewById(R.id.tv_code1); textViews[1] = (TextView) findViewById(R.id.tv_code2); textViews[2] = (TextView) findViewById(R.id.tv_code3); textViews[3] = (TextView) findViewById(R.id.tv_code4); //将光标隐藏 editText.setCursorVisible(false); //监听EditText文本变化 setListener(); } /** * 监听EditText文本变化,获取输入内容,将值赋值给textview */ private void setListener() { editText.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 editable) { // 如果字符不为""时才进行操作 if (!editable.toString().equals("")) { if (stringBuffer.length() > 3) { //当文本长度大于3位时edittext置空 editText.setText(""); return; } else { //将文字添加到StringBuffer中 stringBuffer.append(editable); editText.setText("");//添加后将EditText置空 count = stringBuffer.length();//记录stringbuffer的长度 inputContent = stringBuffer.toString(); if (stringBuffer.length() == 4) { //调用完成输入的监听 if (inputCompleteListener != null) { inputCompleteListener.inputComplete(); } } } for (int i = 0; i < stringBuffer.length(); i++) { textViews[i].setText(String.valueOf(inputContent.charAt(i))); } } } }); editText.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) { if (onKeyDelete()) return true; return true; } return false; } }); } /** * 删除监听 * * @return */ public boolean onKeyDelete() { if (count == 0) { count = 4; return true; } if (stringBuffer.length() > 0) { //删除相应位置的字符 stringBuffer.delete((count - 1), count); count--; inputContent = stringBuffer.toString(); textViews[stringBuffer.length()].setText(""); if (inputCompleteListener != null) inputCompleteListener.deleteContent(true);//有删除就通知manger } return false; } /** * 清空输入内容 */ public void clearEditText() { stringBuffer.delete(0, stringBuffer.length()); inputContent = stringBuffer.toString(); for (int i = 0; i < textViews.length; i++) { textViews[i].setText(""); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); } private InputCompleteListener inputCompleteListener; public void setInputCompleteListener(InputCompleteListener inputCompleteListener) { this.inputCompleteListener = inputCompleteListener; } public interface InputCompleteListener { void inputComplete(); void deleteContent(boolean isDelete); } /** * 获取输入文本 * * @return */ public String getEditContent() { return inputContent; } }在activity里实现接口,重写回调方法:
public class MainActivity extends AppCompatActivity implements VerificationCodeView.InputCompleteListener { VerificationCodeView vcw; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vcw = findViewById(R.id.vc_view); vcw.setInputCompleteListener(this); } @Override public void inputComplete() { //监听输入完成 //关闭输入键盘 ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); Toast.makeText(MainActivity.this, vcw.getEditContent(), Toast.LENGTH_SHORT).show(); } @Override public void deleteContent(boolean isDelete) { Toast.makeText(MainActivity.this, "监听删除", Toast.LENGTH_SHORT).show(); } }下面附上demo下载链接:
https://download.csdn.net/download/shanshan_1117/10422761
“`
