package com.tejia.lijin.app.util.verifycode;
|
|
import android.content.ClipData;
|
import android.content.ClipboardManager;
|
import android.content.Context;
|
import android.text.Editable;
|
import android.text.TextWatcher;
|
import android.text.method.ReplacementTransformationMethod;
|
import android.util.AttributeSet;
|
import android.view.View;
|
import android.widget.EditText;
|
import android.widget.RelativeLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.tejia.lijin.app.R;
|
|
public class VerifySixCodeView extends RelativeLayout {
|
private EditText editText;
|
private TextView[] textViews;
|
private static int MAX = 6;
|
private String inputContent;
|
|
public VerifySixCodeView(Context context) {
|
this(context, null);
|
}
|
|
public VerifySixCodeView(final Context context, AttributeSet attrs) {
|
this(context, attrs, 0);
|
}
|
|
public VerifySixCodeView(final Context context, AttributeSet attrs, int defStyleAttr) {
|
super(context, attrs, defStyleAttr);
|
View.inflate(context, R.layout.view_verify_six_code, this);
|
textViews = new TextView[MAX];
|
textViews[0] = (TextView) findViewById(R.id.tv_0);
|
textViews[1] = (TextView) findViewById(R.id.tv_1);
|
textViews[2] = (TextView) findViewById(R.id.tv_2);
|
textViews[3] = (TextView) findViewById(R.id.tv_3);
|
textViews[4] = (TextView) findViewById(R.id.tv_4);
|
textViews[5] = (TextView) findViewById(R.id.tv_5);
|
editText = (EditText) findViewById(R.id.edit_text_view);
|
|
editText.setCursorVisible(false);//隐藏光标
|
//将输入法切换到英文
|
// editText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
|
//将输入法弹出的右下角的按钮改为完成,不改的话会是下一步。
|
// editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
|
setEditTextListener();
|
//长按粘贴
|
editText.setOnLongClickListener(new OnLongClickListener() {
|
@Override
|
public boolean onLongClick(View v) {
|
setEditTextconent(context, true);
|
return false;
|
}
|
});
|
}
|
|
/**
|
* 设置 文本框 剪切板文字
|
* boo true显示提示 false不显示提示
|
*/
|
public void setEditTextconent(Context context, boolean boo) {
|
// 获取系统剪贴板
|
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
// 获取剪贴板的剪贴数据集
|
ClipData clipData = clipboard.getPrimaryClip();
|
if (clipData != null && clipData.getItemCount() > 0 && clipData.getItemAt(0) != null) {
|
// 从数据集中获取(粘贴)第一条文本数据
|
String text = "";
|
CharSequence cs = clipData.getItemAt(0).getText();
|
if (cs != null)
|
text = cs + "";
|
if (text.length() == 0) {
|
return;
|
}
|
if (text.length() != 6) {
|
if (boo) {
|
Toast.makeText(context, "格式不正确", Toast.LENGTH_SHORT).show();
|
}
|
} else {
|
//仅包含 数字和字母
|
if (isLetterDigit(text.toString())) {
|
editText.setText(text);
|
} else {
|
if (boo) {
|
Toast.makeText(context, "格式不正确", Toast.LENGTH_SHORT).show();
|
}
|
}
|
}
|
// Log.e("eee", "onLongClick: " + clipData.getItemAt(0).getText());
|
} else {
|
if (boo) {
|
Toast.makeText(context, "剪贴板无数据", Toast.LENGTH_SHORT).show();
|
}
|
}
|
}
|
|
/**
|
* 判断 字符串 仅含有数字和字母
|
*
|
* @param str
|
* @return
|
*/
|
public static boolean isLetterDigit(String str) {
|
String regex = "^[a-z0-9A-Z]+$";
|
return str.matches(regex);
|
}
|
|
private void setEditTextListener() {
|
editText.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
}
|
|
@Override
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
}
|
|
@Override
|
public void afterTextChanged(Editable editable) {
|
inputContent = editText.getText().toString().toUpperCase();//小写转大写
|
if (inputCompleteListener != null) {
|
if (inputContent.length() >= MAX) {
|
inputCompleteListener.inputComplete();
|
} else {
|
inputCompleteListener.invalidContent();
|
}
|
}
|
|
for (int i = 0; i < MAX; i++) {
|
if (i < inputContent.length()) {
|
textViews[i].setText(String.valueOf(inputContent.charAt(i)));
|
} else {
|
textViews[i].setText("");
|
}
|
}
|
}
|
});
|
}
|
|
|
private InputCompleteListener inputCompleteListener;
|
|
public void setInputCompleteListener(InputCompleteListener inputCompleteListener) {
|
this.inputCompleteListener = inputCompleteListener;
|
}
|
|
public interface InputCompleteListener {
|
|
void inputComplete();
|
|
void invalidContent();
|
}
|
|
public String getEditContent() {
|
return inputContent;
|
}
|
|
/**
|
* @author bruce.z
|
*/
|
public class AllCapTransformationMethod extends ReplacementTransformationMethod {
|
|
@Override
|
protected char[] getOriginal() {
|
char[] aa = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
|
return aa;
|
}
|
|
@Override
|
protected char[] getReplacement() {
|
char[] cc = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
|
return cc;
|
}
|
|
}
|
}
|