输入验证码页面

main
huangwei 9 months ago
parent c9c1f80d72
commit a5f1960943
  1. 22
      app/src/main/AndroidManifest.xml
  2. 1
      app/src/main/java/com/project/survey/activity/base/BaseBindingActivity.kt
  3. 4
      app/src/main/java/com/project/survey/activity/login/LoginActivity.kt
  4. 21
      app/src/main/java/com/project/survey/activity/login/LoginInputCodeActivity.kt
  5. 2
      app/src/main/java/com/project/survey/fragment/home/MeFragment.kt
  6. 60
      app/src/main/java/com/project/survey/widget/edittext/verificationcode/MyEditText.java
  7. 199
      app/src/main/java/com/project/survey/widget/edittext/verificationcode/SerialnumberLayout.java
  8. 15
      app/src/main/res/drawable/bg_input_code_gray.xml
  9. 15
      app/src/main/res/drawable/bg_input_code_selected.xml
  10. 8
      app/src/main/res/drawable/bg_item_input_code.xml
  11. 41
      app/src/main/res/layout/activity_login_input_code.xml
  12. 18
      app/src/main/res/layout/verifation_code_item.xml
  13. 11
      app/src/main/res/layout/verification_code.xml
  14. 8
      app/src/main/res/values/attr.xml
  15. 1
      app/src/main/res/values/color.xml
  16. 3
      app/src/main/res/values/strings.xml

@ -59,24 +59,18 @@
android:name=".activity.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize|stateHidden"/>
android:windowSoftInputMode="adjustResize|stateHidden" />
<activity
android:name=".activity.LoginActivity"
android:name=".activity.login.LoginActivity"
android:exported="true"
android:windowSoftInputMode="adjustResize|stateHidden"/>
<!-- <activity-->
<!-- android:name="com.bingce.surveyor.agentweb.AgentWebActivity"-->
<!-- android:configChanges="orientation|keyboardHidden|screenSize"-->
<!-- android:exported="true"-->
<!-- android:hardwareAccelerated="true"-->
<!-- android:windowSoftInputMode="adjustUnspecified|stateHidden" />-->
android:windowSoftInputMode="adjustResize|stateHidden" />
<activity
android:name=".activity.login.LoginInputCodeActivity"
android:exported="true"
android:label=""
android:windowSoftInputMode="adjustResize|stateHidden" />
</application>

@ -21,7 +21,6 @@ abstract class BaseBindingActivity<VB : ViewBinding> : AppCompatActivity() {
private var toolBar: Toolbar? = null
abstract fun getBinding(): VB
abstract fun initView()
abstract fun initData()

@ -1,5 +1,6 @@
package com.project.survey.activity
package com.project.survey.activity.login
import android.content.Intent
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
@ -54,6 +55,7 @@ class LoginActivity : BaseBindingActivity<ActivityLoginBinding>() {
*/
private fun loginByInner() {
startActivity(Intent(this, LoginInputCodeActivity::class.java))
}

@ -0,0 +1,21 @@
package com.project.survey.activity.login
import android.util.Log
import com.project.survey.activity.base.BaseBindingActivity
import com.project.survey.databinding.ActivityLoginInputCodeBinding
class LoginInputCodeActivity : BaseBindingActivity<ActivityLoginInputCodeBinding>() {
override fun getBinding(): ActivityLoginInputCodeBinding {
return ActivityLoginInputCodeBinding.inflate(layoutInflater)
}
override fun initView() {
mBinding.etInputCode.setOnInputListener {
Log.d("hwhw", "sss" + it)
}
}
override fun initData() {
}
}

@ -3,7 +3,7 @@ package com.project.survey.fragment.home
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import com.project.survey.activity.LoginActivity
import com.project.survey.activity.login.LoginActivity
import com.project.survey.databinding.FragmentMeBinding
import com.project.survey.fragment.base.BaseFragmentBinding

@ -0,0 +1,60 @@
package com.project.survey.widget.edittext.verificationcode;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import androidx.appcompat.widget.AppCompatEditText;
public class MyEditText extends AppCompatEditText {
public interface onTextContextMenuItemListener {
public boolean onTextContextMenuItem(int id, String content);
}
private onTextContextMenuItemListener onTextContextMenuItemListener;
//设置监听回调
public void setZTListener(onTextContextMenuItemListener listener) {
this.onTextContextMenuItemListener = listener;
}
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyEditText(Context context) {
super(context);
}
@Override
public boolean onTextContextMenuItem(int id) {
if (id == android.R.id.paste) {
ClipboardManager clip = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
onTextContextMenuItemListener.onTextContextMenuItem(id, clip.getText().toString());
}
return false;
}
/**
* 屏蔽回车
*
* @param event
* @return
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
return true;
}
return super.dispatchKeyEvent(event);
}
}

@ -0,0 +1,199 @@
package com.project.survey.widget.edittext.verificationcode;
import static blankj.utilcode.util.ResourceUtils.getDrawable;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.project.survey.R;
import com.project.survey.util.Tools;
import java.util.ArrayList;
import java.util.List;
public class SerialnumberLayout extends RelativeLayout {
private Context context;
List<MyEditText> editViews;
private int textColor;
private int codeNumber;
private LinearLayout ll_content;
public SerialnumberLayout(Context context) {
this(context, null);
}
public SerialnumberLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SerialnumberLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
loadView(attrs);
}
private void loadView(AttributeSet attrs) {
View view = LayoutInflater.from(context).inflate(R.layout.verification_code, this);
ll_content = view.findViewById(R.id.ll_code_content);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Verificationcode);
textColor = typedArray.getColor(R.styleable.Verificationcode_code_text_color, Tools.getColor(R.color.text_color_1));
codeNumber = typedArray.getInt(R.styleable.Verificationcode_code_number, 16);
if (codeNumber > 8 && codeNumber % 2 == 1) codeNumber += 1;
initView();
}
private void initView() {
editViews = new ArrayList<>();
LinearLayout linearLayout1 = new LinearLayout(context);
LinearLayout linearLayout2 = new LinearLayout(context);
for (int i = 0; i < codeNumber; i++) {
LinearLayout.LayoutParams layout_param = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
View item_view = LayoutInflater.from(context).inflate(R.layout.verifation_code_item, null);
final MyEditText code_child = item_view.findViewById(R.id.tv_code);
code_child.setTextColor(textColor);
code_child.setBackground(getDrawable(R.drawable.bg_input_code_gray));
code_child.setId(i);
code_child.setOnFocusChangeListener((view, b) -> {
if (b == true) {
code_child.setBackground(getDrawable(R.drawable.bg_input_code_selected));
} else {
code_child.setBackground(getDrawable(R.drawable.bg_input_code_gray));
}
});
code_child.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable editable) {
if (editable != null && editable.length() > 0) {
String inputcontent = editable.toString();
int location = code_child.getId();
if (location + inputcontent.length() <= codeNumber) {
if (inputcontent.length() > 1 && location < codeNumber - 1) {
for (int i = location; i < editViews.size(); i++) {
MyEditText myEditText = editViews.get(i);
myEditText.setText("");
}
for (int i = 0; i < inputcontent.length(); i++) {
showCode(i + location, inputcontent.charAt(i) + "");
}
editViews.get(location + inputcontent.length() - 1).setSelection(1);
} else {
if (location < codeNumber - 1) {
showCode(location + 1, "");
code_child.setBackground(getDrawable(R.drawable.bg_input_code_selected));
} else {
String content = "";
for (int i = 0; i < codeNumber; i++) {
content += editViews.get(i).getText();
}
if (onInputListener != null)
onInputListener.onSucess(content);
}
}
} else {
code_child.setText("");
Toast.makeText(context, "长度超过" + codeNumber + ",请检查", Toast.LENGTH_SHORT).show();
}
}
}
});
// 监听验证码删除按键
code_child.setOnKeyListener((view, keyCode, keyEvent) -> {
if (keyCode == KeyEvent.KEYCODE_DEL && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
int location = code_child.getId();
if (code_child.getText().toString().equals("")) {
if (location >= 1)
removeCode(location - 1);
return true;
} else
return false;
}
return false;
});
code_child.setZTListener((id, content) -> {
if (content.length() > codeNumber) {
Toast.makeText(context, "长度超过" + codeNumber + ",请检查", Toast.LENGTH_SHORT).show();
} else if (content.length() > 0) {
for (int i1 = 0; i1 < editViews.size(); i1++) {
MyEditText myEditText = editViews.get(i1);
myEditText.setText("");
}
showCode(0, "");
for (int i1 = 0; i1 < content.length(); i1++) {
showCode(i1, content.charAt(i1) + "");
}
editViews.get(content.length() - 1).setSelection(1);
}
return false;
});
if (codeNumber <= 8) {
linearLayout1.addView(item_view, layout_param);
} else {
if (i >= codeNumber / 2) {
linearLayout2.addView(item_view, layout_param);
} else
linearLayout1.addView(item_view, layout_param);
}
code_child.setInputType(InputType.TYPE_CLASS_NUMBER);
editViews.add(code_child);
}
if (codeNumber <= 8) {
ll_content.addView(linearLayout1);
} else {
ll_content.addView(linearLayout1);
ll_content.addView(linearLayout2);
}
InputFilter[] filters = {new InputFilter.LengthFilter(1)};
editViews.get(codeNumber - 1).setFilters(filters);
editViews.get(0).setFocusable(true);
editViews.get(0).setFocusableInTouchMode(true);
editViews.get(0).requestFocus();
}
private void showCode(int location, String code) {
EditText et_code = editViews.get(location);
et_code.setFocusable(true);
et_code.setFocusableInTouchMode(true);
et_code.requestFocus();
et_code.setText(code);
}
private void removeCode(int location) {
EditText et_code = editViews.get(location);
et_code.setFocusable(true);
et_code.setFocusableInTouchMode(true);
et_code.requestFocus();
et_code.setText("");
}
private OnInputListener onInputListener;
//定义回调
public interface OnInputListener {
void onSucess(String code);
}
public void setOnInputListener(OnInputListener onInputListener) {
this.onInputListener = onInputListener;
}
}

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 背景颜色 -->
<solid android:color="@color/white" />
<!-- 边框线 -->
<stroke
android:width="1dp"
android:color="@color/c_727778" />
<!-- 圆角 -->
<corners android:radius="8dp" />
</shape>

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 背景颜色 -->
<solid android:color="@color/white" />
<!-- 边框线 -->
<stroke
android:width="1dp"
android:color="@color/colorPrimary" />
<!-- 圆角 -->
<corners android:radius="8dp" />
</shape>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="#00ffffff" />
<stroke
android:width="1dp"
android:color="@color/colorPrimary" />
</shape>

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<TextView
android:id="@+id/tvAccountDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/sw_22dp"
android:layout_marginTop="@dimen/sw_55dp"
android:text="@string/please_input_code"
android:textColor="@color/text_color_1"
android:textSize="@dimen/sw_20sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/sw_22dp"
android:layout_marginTop="@dimen/sw_6dp"
android:text="@string/the_verification_code_was_sent_to_phone"
android:textColor="@color/c_727778"
android:textSize="@dimen/sw_14sp" />
<com.project.survey.widget.edittext.verificationcode.SerialnumberLayout
android:id="@+id/etInputCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/sw_22dp"
android:layout_marginTop="@dimen/sw_24dp"
app:code_number="6"
app:line_color_default="@color/c_dfdfdf"
app:line_color_focus="@color/colorPrimary" />
</LinearLayout>

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="3dp"
android:layout_weight="1">
<com.project.survey.widget.edittext.verificationcode.MyEditText
android:id="@+id/tv_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"
android:textSize="25sp"
android:background="@drawable/bg_item_input_code"/>
</RelativeLayout>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll_code_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</RelativeLayout>

@ -67,4 +67,12 @@
<attr name="maxLine" format="integer"/>
</declare-styleable>
<declare-styleable name="Verificationcode">
<attr format="color" name="code_text_color"/>//验证码字体颜色
<attr format="dimension" name="code_text_size"/>//验证码字体大小
<attr format="integer" name="code_number"/>//验证码数量 4位 6位
<attr format="color" name="line_color_default"/>//验证码下面线的默认颜色
<attr format="color" name="line_color_focus"/>//验证码下面线选中后的颜色
</declare-styleable>
</resources>

@ -11,6 +11,7 @@
<color name="c_2964da">#2964DA</color>
<color name="c_ebebeb">#EBEBEB</color>
<color name="c_727778">#727778</color>
<color name="c_dfdfdf">#DFDFDF</color>
</resources>

@ -38,5 +38,8 @@
<string name="login_account_or_password_error_hint">您的账号或密码输入有误,请重新输入。</string>
<string name="xui_met_input_error" translatable="false">输入不符合要求!</string>
<string name="xui_met_not_allow_empty" translatable="false">输入不能为空!</string>
<string name="please_input_code">请输入验证码</string>
<string name="the_verification_code_was_sent_to_phone">验证码已发送至%1$s的手机,如您已更换手机,请联系管理员。</string>
</resources>
Loading…
Cancel
Save