parent
c9c1f80d72
commit
a5f1960943
16 changed files with 412 additions and 17 deletions
@ -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() { |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -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> |
Loading…
Reference in new issue