简单的验证码

自定义控件

public class VerifyCodeViewextends RelativeLayout{

private EditTexteditText;

private TextView[]textViews;

private static int MAX =4;

private StringinputContent;

public VerifyCodeView(Context context) {

this(context,null);

}

public VerifyCodeView(Context context, AttributeSet attrs) {

this(context,attrs,0);

}

public VerifyCodeView(Context context, AttributeSet attrs,int defStyleAttr) {

super(context, attrs, defStyleAttr);

View.inflate(context, R.layout.view_verity_code,this);

textViews =new TextView[MAX];

textViews[0] =  findViewById(R.id.tv_0);

textViews[1] =  findViewById(R.id.tv_1);

textViews[2] =  findViewById(R.id.tv_2);

textViews[3] =  findViewById(R.id.tv_3);

editText =  findViewById(R.id.edit_text_view);

editText.setCursorVisible(false);//隐藏光标

        setEditTextListener();

}

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();

if (inputCompleteListener !=null) {

if (inputContent.length() >=MAX) {

inputCompleteListener.inputComplete();

}else {

inputCompleteListener.invalidContent();

}

}

for (int i =0; i

if (i

textViews[i].setText(String.valueOf(inputContent.charAt(i)));

}else {

textViews[i].setText("");

}

}

}

});

}

public VerifyCodeView(Context context, AttributeSet attrs,int defStyleAttr,int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

}

private InputCompleteListenerinputCompleteListener;

public void setInputCompleteListener(InputCompleteListener inputCompleteListener) {

this.inputCompleteListener = inputCompleteListener;

}

public interface InputCompleteListener {

void inputComplete();

void invalidContent();

}

public String getEditContent() {

return inputContent;

}

}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content">

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="48dp"

        android:gravity="center"

        android:orientation="horizontal">

        <TextView

            android:id="@+id/tv_0"

            style="@style/textview_style"

            />

        <View

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

        <TextView

            android:id="@+id/tv_1"

            style="@style/textview_style" />

        <View

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

        <TextView

            android:id="@+id/tv_2"

            style="@style/textview_style" />

        <View

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

        <TextView

            android:id="@+id/tv_3"

            style="@style/textview_style"

            />

    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="48dp"

        android:gravity="center"

        android:orientation="horizontal">

        <View

            android:layout_weight="1"

            android:layout_width="0dp"

            android:layout_height="2dp"

            android:layout_gravity="bottom"

            android:background="@color/color_white" />

        <View

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

        <View

            android:layout_weight="1"

            android:layout_width="0dp"

            android:layout_height="2dp"

            android:layout_gravity="bottom"

            android:background="@color/color_white" />

        <View

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

        <View

            android:layout_weight="1"

            android:layout_width="0dp"

            android:layout_height="2dp"

            android:layout_gravity="bottom"

            android:background="@color/color_white" />

        <View

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

        <View

            android:layout_weight="1"

            android:layout_width="0dp"

            android:layout_height="2dp"

            android:layout_gravity="bottom"

            android:background="@color/color_white" />

        <View

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

        <View

            android:layout_weight="1"

            android:layout_width="0dp"

            android:layout_height="2dp"

            android:layout_gravity="bottom"

            android:background="@color/color_white" />

        <View

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

        <View

            android:layout_weight="1"

            android:layout_width="0dp"

            android:layout_height="2dp"

            android:layout_gravity="bottom"

            android:background="@color/color_white" />

    </LinearLayout>

    <MyEditText

        android:id="@+id/edit_text_view"

        android:layout_width="match_parent"

        android:layout_height="48dp"

        android:background="@android:color/transparent"

        android:inputType="number"

        android:longClickable="false"

        android:maxLength="4"

        android:textColor="@color/transparent" />

</RelativeLayout>


MyEditText

public class MyEditTextextends AppCompatEditText {

private long lastTime =0;

public MyEditText(Context context) {

super(context);

}

public MyEditText(Context context, AttributeSet attrs) {

super(context, attrs);

}

public MyEditText(Context context, AttributeSet attrs,int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override

    protected void onSelectionChanged(int selStart,int selEnd) {

super.onSelectionChanged(selStart, selEnd);

this.setSelection(this.getText().length());

}

@Override

    public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

long currentTime = System.currentTimeMillis();

if (currentTime -lastTime <500) {

lastTime = currentTime;

return true;

}else {

lastTime = currentTime;

}

break;

}

return super.onTouchEvent(event);

}

}


Style

<style name="textview_style">

        <item name="android:layout_height">48dp</item>

        <item name="android:layout_width">0dp</item>

        <item name="android:background">@color/transparent</item>

        <item name="android:gravity">center</item>

        <item name="android:textColor">@color/black</item>

        <item name="android:textSize">15sp</item>

        <item name="android:textStyle">normal</item>

    </style>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容