admin
2021-06-05 ddff7888bf7e754d12fb5fc85a58f3012f456490
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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;
        }
 
    }
}