update 添加onTextChange接口

This commit is contained in:
jiaoyaning
2019-08-08 18:42:41 +08:00
parent 5e73d38631
commit 3a94631cfc
3 changed files with 32 additions and 37 deletions

View File

@@ -21,8 +21,13 @@ public class MainActivity extends AppCompatActivity {
textView = findViewById(R.id.text);
verificationcodeview.setOnCodeFinishListener(new VerificationCodeView.OnCodeFinishListener() {
@Override
public void onComplete(String content) {
textView.setText(content);
public void onTextChange(View view, String content) {
textView.setText("输入中:" + content);
}
@Override
public void onComplete(View view, String content) {
textView.setText("输入结束:" + content);
}
});
}

View File

@@ -11,7 +11,7 @@
android:id="@+id/verificationcodeview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal"
app:vcv_et_bg="@drawable/et_login_code"
@@ -22,26 +22,19 @@
app:vcv_et_text_color="@android:color/black"
app:vcv_et_text_size="6sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:visibility="gone" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="验证码"
android:textSize="20sp" />
android:text="输入开始:" />
<Button
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginTop="10dp"
android:onClick="clear"
android:text="清空输入框" />
</LinearLayout>

View File

@@ -27,10 +27,9 @@ import java.lang.reflect.Field;
* update 2019/8/8
*/
public class VerificationCodeView extends LinearLayout implements TextWatcher, View.OnKeyListener, View.OnFocusChangeListener {
public class VerificationCodeView extends LinearLayout implements TextWatcher, View.OnKeyListener {
private Context mContext;
private long endTime = 0;
private OnCodeFinishListener onCodeFinishListener;
/**
@@ -251,7 +250,6 @@ public class VerificationCodeView extends LinearLayout implements TextWatcher, V
editText.setBackgroundResource(mEtTextBg);
setEditTextCursorDrawable(editText);
editText.addTextChangedListener(this);
editText.setOnFocusChangeListener(this);
editText.setOnKeyListener(this);
}
@@ -333,6 +331,14 @@ public class VerificationCodeView extends LinearLayout implements TextWatcher, V
if (s.length() != 0) {
focus();
}
if (onCodeFinishListener != null) {
onCodeFinishListener.onTextChange(this, getResult());
//如果最后一个输入框有字符,则返回结果
EditText lastEditText = (EditText) getChildAt(mEtNumber - 1);
if (lastEditText.getText().length() > 0) {
onCodeFinishListener.onComplete(this, getResult());
}
}
}
@Override
@@ -376,12 +382,6 @@ public class VerificationCodeView extends LinearLayout implements TextWatcher, V
}
}
}
//如果最后一个输入框有字符,则返回结果
EditText lastEditText = (EditText) getChildAt(mEtNumber - 1);
if (lastEditText.getText().length() > 0) {
getResult();
}
}
private void backFocus() {
@@ -402,27 +402,24 @@ public class VerificationCodeView extends LinearLayout implements TextWatcher, V
}
}
private void getResult() {
StringBuffer stringBuffer = new StringBuffer();
private String getResult() {
StringBuilder stringBuffer = new StringBuilder();
EditText editText;
for (int i = 0; i < mEtNumber; i++) {
editText = (EditText) getChildAt(i);
stringBuffer.append(editText.getText());
}
if (onCodeFinishListener != null) {
onCodeFinishListener.onComplete(stringBuffer.toString());
}
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
focus();
}
return stringBuffer.toString();
}
public interface OnCodeFinishListener {
void onComplete(String content);
void onTextChange(View view, String content);
/**
* 输入完成
*/
void onComplete(View view, String content);
}
/**