很多软件登录时都有那种间断的edittext,boss也提了这个需求,那就来实现一把咯。
话不多说,先上代码:
//自定义一个EditText重写onTextContextMenuItem()方法
@SuppressLint("AppCompatCustomView")
public class LoginChildEditText extends EditText {
private PasteListener pasteListener;
public void setPasteListener(PasteListener pasteListener){
this.pasteListener = pasteListener;
}
public LoginChildEditText(Context context) {
super(context);
}
public LoginChildEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LoginChildEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTextContextMenuItem(int id) {
if(id==android.R.id.paste){
ClipboardManager clip = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE); //获取粘贴板
if (clip != null) {
String text = clip.getPrimaryClip().getItemAt(0).getText().toString(); //获取粘贴内容
pasteListener.setPasteText(this,text); //设置一个接口,由LoginChildEditText在外部实现
}
return true;
}else {
return super.onTextContextMenuItem(id);
}
}
public interface PasteListener{
void setPasteText(View view, String text);
}
}
//自定义一个LinearLayout,装载LoginChildEditText实现输入框样式,如下图所示,
public class LoginEditText extends LinearLayout implements View.OnKeyListener, View.OnFocusChangeListener, TextWatcher,LoginChildEditText.PasteListener {
LoginChildEditText code1;
LoginChildEditText code2;
LoginChildEditText code3;
LoginChildEditText code4;
private int editInt;
public void requestCodeFocus(){
if(code1!=null){
code1.setFocusable(true);
code1.requestFocus();
}
}
public String getString(){
return code1.getText().toString() + code2.getText().toString()
+ code3.getText().toString() + code4.getText().toString();
}
public LoginEditText(Context context) {
super(context);
}
public LoginEditText(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public LoginEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_login_identity_edit, this);
code1 = view.findViewById(R.id.code_1);
code2 = view.findViewById(R.id.code_2);
code3 = view.findViewById(R.id.code_3);
code4 = view.findViewById(R.id.code_4);
code1.setOnKeyListener(this);
code2.setOnKeyListener(this);
code3.setOnKeyListener(this);
code4.setOnKeyListener(this);
code1.setOnFocusChangeListener(this);
code2.setOnFocusChangeListener(this);
code3.setOnFocusChangeListener(this);
code4.setOnFocusChangeListener(this);
code1.addTextChangedListener(this);
code2.addTextChangedListener(this);
code3.addTextChangedListener(this);
code4.addTextChangedListener(this);
code1.setPasteListener(this);
code2.setPasteListener(this);
code3.setPasteListener(this);
code4.setPasteListener(this);
InputMethodManager inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null) {
inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
@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) {
switch (editInt) {
case 1:
if (editable.toString().length() == 1) {
code2.setFocusable(true);
code2.requestFocus();
}
break;
case 2:
if (editable.toString().length() == 1) {
code3.setFocusable(true);
code3.requestFocus();
}
break;
case 3:
if (editable.toString().length() == 1) {
code4.setFocusable(true);
code4.requestFocus();
}
break;
case 4:
break;
default:
break;
}
}
@Override
public void onFocusChange(View view, boolean b) {
switch (view.getId()) {
case R.id.code_1:
if (b) {
editInt = 1;
code1.setSelection(code1.getText().toString().length());
}
break;
case R.id.code_2:
if (b) {
editInt = 2;
code2.setSelection(code2.getText().toString().length());
}
break;
case R.id.code_3:
if (b) {
editInt = 3;
code3.setSelection(code3.getText().toString().length());
}
break;
case R.id.code_4:
if (b) {
editInt = 4;
code4.setSelection(code4.getText().toString().length());
}
break;
default:
break;
}
}
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (KeyEvent.KEYCODE_DEL == keyEvent.getKeyCode() && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
switch (editInt) {
case 1:
break;
case 2:
code1.setFocusable(true);
code1.requestFocus();
break;
case 3:
code2.setFocusable(true);
code2.requestFocus();
break;
case 4:
code3.setFocusable(true);
code3.requestFocus();
break;
default:
break;
}
}
return false;
}
//重写setPasteText完成复制功能
@Override
public void setPasteText(View view,String text) {
Logger.d(text);
if(view.getId()== R.id.code_1||view.getId()== R.id.code_2||view.getId()== R.id.code_3||view.getId()== R.id.code_4){
Logger.d(String.valueOf(text.charAt(0)));
if(text.length()==1){
code1.setText(String.valueOf(text.charAt(0)));
code2.setFocusable(true);
code2.requestFocus();
}else if(text.length()==2){
code1.setText(String.valueOf(text.charAt(0)));
code2.setText(String.valueOf(text.charAt(1)));
code3.setFocusable(true);
code3.requestFocus();
}else if(text.length()==3){
code1.setText(String.valueOf(text.charAt(0)));
code2.setText(String.valueOf(text.charAt(1)));
code3.setText(String.valueOf(text.charAt(2)));
code4.setFocusable(true);
code4.requestFocus();
}else if(text.length()>=4){
code1.setText(String.valueOf(text.charAt(0)));
code2.setText(String.valueOf(text.charAt(1)));
code3.setText(String.valueOf(text.charAt(2)));
code4.setText(String.valueOf(text.charAt(3)));
code4.setSelection(code4.getText().toString().length());
}
}
}
}
// R.layout.layout_login_identity_edit 布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="15dp"> <com.qitiancp.customview.LoginChildEditText android:id="@+id/code_1" android:layout_width="31dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp" android:background="@null" android:gravity="center" android:includeFontPadding="false" android:inputType="number" android:maxLength="1" android:nextFocusDown="@id/code_2" android:singleLine="true" android:text="" android:textColor="@color/black" android:textSize="29sp" /> <ImageView android:layout_width="31dp" android:layout_height="2dp" android:layout_below="@id/code_1" android:scaleType="fitXY" android:src="@drawable/underline" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="15dp"> <com.qitiancp.customview.LoginChildEditText android:id="@+id/code_2" android:layout_width="31dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp" android:background="@null" android:gravity="center" android:includeFontPadding="false" android:inputType="number" android:maxLength="1" android:nextFocusDown="@id/code_3" android:singleLine="true" android:text="" android:textColor="@color/black" android:textSize="29sp" /> <ImageView android:layout_width="31dp" android:layout_height="2dp" android:layout_below="@id/code_2" android:src="@drawable/underline" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="15dp"> <com.qitiancp.customview.LoginChildEditText android:id="@+id/code_3" android:layout_width="31dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp" android:background="@null" android:gravity="center" android:includeFontPadding="false" android:inputType="number" android:maxLength="1" android:nextFocusDown="@id/code_4" android:singleLine="true" android:text="" android:textColor="@color/black" android:textSize="29sp" /> <ImageView android:layout_width="31dp" android:layout_height="2dp" android:layout_below="@id/code_3" android:src="@drawable/underline" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="15dp"> <com.qitiancp.customview.LoginChildEditText android:id="@+id/code_4" android:layout_width="31dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp" android:background="@null" android:gravity="center" android:includeFontPadding="false" android:inputType="number" android:maxLength="1" android:singleLine="true" android:text="" android:textColor="@color/black" android:textSize="29sp" /> <ImageView android:layout_width="31dp" android:layout_height="2dp" android:layout_below="@id/code_4" android:src="@drawable/underline" /> </RelativeLayout> </LinearLayout>
ok!