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); textView = findViewById(R.id.text);
verificationcodeview.setOnCodeFinishListener(new VerificationCodeView.OnCodeFinishListener() { verificationcodeview.setOnCodeFinishListener(new VerificationCodeView.OnCodeFinishListener() {
@Override @Override
public void onComplete(String content) { public void onTextChange(View view, String content) {
textView.setText(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:id="@+id/verificationcodeview"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="30dp" android:layout_marginTop="10dp"
android:gravity="center" android:gravity="center"
android:orientation="horizontal" android:orientation="horizontal"
app:vcv_et_bg="@drawable/et_login_code" app:vcv_et_bg="@drawable/et_login_code"
@@ -22,26 +22,19 @@
app:vcv_et_text_color="@android:color/black" app:vcv_et_text_color="@android:color/black"
app:vcv_et_text_size="6sp" /> app:vcv_et_text_size="6sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:visibility="gone" />
<TextView <TextView
android:id="@+id/text" android:id="@+id/text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
android:text="验证码" android:text="输入开始:" />
android:textSize="20sp" />
<Button <Button
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_marginTop="10dp"
android:layout_marginTop="20dp"
android:onClick="clear" android:onClick="clear"
android:text="清空输入框" /> android:text="清空输入框" />
</LinearLayout> </LinearLayout>

View File

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