Android 사용자 정의 사각형 EditText 등록 인증 코드

먼저 효과 도 를 보 여 드 리 겠 습 니 다.지금 많은 app 들 이 비슷 한 등록 페이지 를 사용 하고 있 습 니 다.
这里写图片描述
사고의 방향 을 실현 하 다.
  • 투명 한 EditText 로 네 개의 TextView 와 겹 치고 TextView 에 기본 배경 을 설정 합 니 다
  • 네 번 째 TextView 입력 이 완료 되면 리 셋 을 설정 하고 삭 제 된 리 셋 을 추가 해 야 합 니 다
  • 또한 EditText 내용 의 변 화 를 감청 하고 내용 을 가 져 오 며 EditText 아래 의 TextView 색상 을 바 꿉 니 다
  • 다시 보 내 는 것 은 사용자 정의 Countdown Timer 류 를 사용 하 는 것 입 니 다
  • 팝 업 효과 사용자 정의 Dialog 계승 DialogFragmentEditText 레이아웃 사용자 정의
    
    <?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:layout_width="wrap_content"
      android:layout_height="47dp"
      android:gravity="center"
      android:orientation="horizontal"
      android:weightSum="3">
    
    
      <TextView
       android:id="@+id/item_code_iv1"
       style="@style/text_editStyle" />
    
      <View
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1" />
    
      <TextView
       android:id="@+id/item_code_iv2"
       style="@style/text_editStyle" />
    
      <View
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1" />
    
      <TextView
       android:id="@+id/item_code_iv3"
       style="@style/text_editStyle" />
    
      <View
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1" />
    
      <TextView
       android:id="@+id/item_code_iv4"
       style="@style/text_editStyle" />
    
    
     </LinearLayout>
    
     <EditText
      android:id="@+id/item_edittext"
      android:layout_width="match_parent"
      android:layout_height="47dp"
      android:background="@android:color/transparent"
      android:inputType="number" />
    </RelativeLayout>
    style
    
     <style name="text_editStyle" >
      <item name="android:layout_height">47dp</item>
      <item name="android:layout_width">47dp</item>
      <item name="android:background">@mipmap/bg_verify</item>
      <item name="android:gravity">center</item>
      <item name="android:textColor">@color/common_blue_0090FF</item>
      <item name="android:textSize">18sp</item>
    
    </style>
    View 코드
    
    private EditText editText;
     private TextView[] TextViews;
     private StringBuffer stringBuffer = new StringBuffer();
     private int count = 4;
     private String inputContent;
    
     public SecurityCodeView(Context context) {
      this(context, null);
     }
    
     public SecurityCodeView(Context context, AttributeSet attrs) {
      this(context, attrs, 0);
     }
    
     public SecurityCodeView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      TextViews = new TextView[4];
      View.inflate(context, R.layout.view_security_code, this);
    
      editText = (EditText) findViewById(R.id.item_edittext);
      TextViews[0] = (TextView) findViewById(R.id.item_code_iv1);
      TextViews[1] = (TextView) findViewById(R.id.item_code_iv2);
      TextViews[2] = (TextView) findViewById(R.id.item_code_iv3);
      TextViews[3] = (TextView) findViewById(R.id.item_code_iv4);
    
      editText.setCursorVisible(false);//     
      setListener();
     }
    
     /**
      *       
      */
     public void clearEditText() {
      stringBuffer.delete(0, stringBuffer.length());
      inputContent = stringBuffer.toString();
      for (int i = 0; i < TextViews.length; i++) {
       TextViews[i].setText("");
       TextViews[i].setBackgroundResource(R.mipmap.bg_verify);
      }
     }
    
     private InputCompleteListener inputCompleteListener;
    
     public void setInputCompleteListener(InputCompleteListener inputCompleteListener) {
      this.inputCompleteListener = inputCompleteListener;
     }
    
     public interface InputCompleteListener {
      void inputComplete();
    
      void deleteContent(boolean isDelete);
     }
    
     /**
      *       
      *
      * @return
      */
     public String getEditContent() {
      return inputContent;
    }
    감청 코드
    
     private void setListener() {
      editText.addTextChangedListener(new TextWatcher() {
    
       @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) {
        //         ""      
        if (!editable.toString().equals("")) {
         if (stringBuffer.length() > 3) {
          //       3  edittext  
          editText.setText("");
          return;
         } else {
          //      StringBuffer 
          stringBuffer.append(editable);
          editText.setText("");//    EditText              
          // Log.e("TAG", "afterTextChanged: stringBuffer is " + stringBuffer);
          count = stringBuffer.length();//  stringbuffer   
          inputContent = stringBuffer.toString();
          if (stringBuffer.length() == 4) {
           //     4           
           if (inputCompleteListener != null) {
            inputCompleteListener.inputComplete();
           }
          }
         }
    
         for (int i = 0; i < stringBuffer.length(); i++) {
          TextViews[i].setText(String.valueOf(inputContent.charAt(i)));
          TextViews[i].setBackgroundResource(R.mipmap.bg_verify_press);
         }
    
        }
       }
      });
    
      editText.setOnKeyListener(new OnKeyListener() {
       @Override
       public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DEL
          && event.getAction() == KeyEvent.ACTION_DOWN) {
         if (onKeyDelete()) return true;
         return true;
        }
        return false;
       }
      });
     }
    
    
     public boolean onKeyDelete() {
      if (count == 0) {
       count = 4;
       return true;
      }
      if (stringBuffer.length() > 0) {
       //         
       stringBuffer.delete((count - 1), count);
       count--;
       // Log.e(TAG, "afterTextChanged: stringBuffer is " + stringBuffer);
       inputContent = stringBuffer.toString();
       TextViews[stringBuffer.length()].setText("");
       TextViews[stringBuffer.length()].setBackgroundResource(R.mipmap.bg_verify);
       if (inputCompleteListener != null)
        inputCompleteListener.deleteContent(true);//      manger
    
      }
      return false;
     }
    사용자 정의 EditText 는 여기까지 입 니 다.끝 난 셈 입 니 다.
    팝 업 상자 의 레이아웃
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/activity_main"
     android:layout_width="283dp"
     android:layout_height="273dp"
     android:layout_gravity="center"
     android:layout_marginBottom="50dp"
     android:background="@mipmap/bg_view1"
     android:orientation="vertical">
    
     <include layout="@layout/layout_titile" />
    
     <com.example.admin.myapplication.SecurityCodeView
      android:id="@+id/scv_edittext"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"
      android:layout_marginTop="35dp" />
    
     <TextView
      android:id="@+id/tv_text"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"
      android:layout_marginTop="13dp"
      android:text="         《    》" />
    
     <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginBottom="25dp"
      android:layout_marginTop="3dp"
      android:orientation="horizontal">
    
      <TextView
    
       android:id="@+id/tv_phone"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_marginLeft="20dp"
       android:layout_marginRight="20dp"
       android:layout_marginTop="13dp"
       android:layout_weight="1"
       android:text="  " />
    
      <TextView
       android:id="@+id/tv_click"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="20dp"
       android:layout_marginRight="20dp"
       android:layout_marginTop="13dp"
       android:text="    "
       android:textColor="@color/colorPrimary" />
     </LinearLayout>
    
    
    </LinearLayout>
    [대체적인 사고방식 으로 이 벤트 를 클릭 한 후에 Dialog 를 팝 업 한 다음 에 이 페이지 를 등록 하면 이 Dialog 가 복 용 될 수도 있 고 스타일 을 바 꿀 수도 있 습 니 다.(Builder 디자인 모드 사용)]
    다음 사용자 정의 Dialog
    EditText 의 두 인 터 페 이 스 를 실현 하려 면
    
    public class XyAlertDialog extends DialogFragment implements SecurityCodeView.InputCompleteListener {
     private SecurityCodeView editText;
     private TextView text;
     private TextView tv_title;
     private ImageView img_close;
     public static final String TAG = XyAlertDialog.class.getSimpleName();
     private Builder builder;
     private static XyAlertDialog instance = new XyAlertDialog();
     private TextView tv_phone;
     private TextView tv_click;
    
     public static XyAlertDialog getInstance() {
      return instance;
     }
    
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
      this.setCancelable(true);
      setRetainInstance(true);
      super.onCreate(savedInstanceState);
    
      if (savedInstanceState != null) {
       try {
        if (isAdded() && getActivity() != null)
         if (builder != null)
          builder = (Builder) savedInstanceState.getSerializable(Builder.class.getSimpleName());
       } catch (Exception e) {
       }
    
      }
     }
    
     @Override
     public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      try {
       if (isAdded() && getActivity() != null)
        if (builder != null)
         outState.putSerializable(Builder.class.getSimpleName(), builder);
      } catch (Exception e) {
       Log.d(TAG, e.toString());
      }
     }
    
     @NonNull
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
      Dialog dialog = super.onCreateDialog(savedInstanceState);
      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
      dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
      dialog.setCanceledOnTouchOutside(false);//       
      return dialog;
     }
    
     @Nullable
     @Override
     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
       container, @Nullable Bundle savedInstanceState) {
      return inflater.inflate(R.layout.activity_xia, container, false);
     }
    
     @Override
     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      initViews(view);
    
     }
    
     private Dialog show(Activity activity, Builder builder) {
      this.builder = builder;
      if (!isAdded())
       show(((AppCompatActivity) activity).getSupportFragmentManager(), TAG);
      return getDialog();
     }
    
     private void initViews(View view) {
      tv_title = (TextView) view.findViewById(R.id.tv_title);
      img_close = (ImageView) view.findViewById(R.id.img_close);
      editText = (SecurityCodeView) view.findViewById(R.id.scv_edittext);
      text = (TextView) view.findViewById(R.id.tv_text);
      tv_phone = (TextView) view.findViewById(R.id.tv_phone);
      tv_click = (TextView) view.findViewById(R.id.tv_click);
      editText.setInputCompleteListener(this);
      tv_phone.setText(builder.getTextTitle());
      img_close.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
        dismiss();
       }
      });
      tv_click.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tv_click, 60000, 1000);
        mCountDownTimerUtils.start();
       }
      });
     }
    
     public static class Builder implements Serializable {
    
      private String positiveButtonText;
      private String negativeButtonText;
      private String textTitle;
      private String body;
    
      private OnPositiveClicked onPositiveClicked;
      private OnNegativeClicked onNegativeClicked;
      private boolean autoHide;
      private int timeToHide;
      private int positiveTextColor;
      private int backgroundColor;
      private int negativeColor;
      private int titleColor;
      private int bodyColor;
      private Typeface titleFont;
      private Typeface bodyFont;
      private Typeface positiveButtonFont;
      private Typeface negativeButtonFont;
    
      private Typeface alertFont;
      private Context context;
      private PanelGravity buttonsGravity;
    
      public PanelGravity getButtonsGravity() {
       return buttonsGravity;
      }
    
      public Builder setButtonsGravity(PanelGravity buttonsGravity) {
       this.buttonsGravity = buttonsGravity;
       return this;
      }
    
      public Typeface getAlertFont() {
       return alertFont;
      }
    
      public Builder setAlertFont(String alertFont) {
       this.alertFont = Typeface.createFromAsset(context.getAssets(), alertFont);
       return this;
      }
    
      public Typeface getPositiveButtonFont() {
       return positiveButtonFont;
      }
    
      public Builder setPositiveButtonFont(String positiveButtonFont) {
       this.positiveButtonFont = Typeface.createFromAsset(context.getAssets(), positiveButtonFont);
       return this;
      }
    
      public Typeface getNegativeButtonFont() {
       return negativeButtonFont;
      }
    
      public Builder setNegativeButtonFont(String negativeButtonFont) {
       this.negativeButtonFont = Typeface.createFromAsset(context.getAssets(), negativeButtonFont);
       return this;
      }
    
      public Typeface getTitleFont() {
       return titleFont;
      }
    
    
      public Builder setTitleFont(String titleFontPath) {
       this.titleFont = Typeface.createFromAsset(context.getAssets(), titleFontPath);
       return this;
      }
    
      public Typeface getBodyFont() {
       return bodyFont;
      }
    
      public Builder setBodyFont(String bodyFontPath) {
       this.bodyFont = Typeface.createFromAsset(context.getAssets(), bodyFontPath);
       return this;
      }
    
    
      public int getTimeToHide() {
       return timeToHide;
      }
    
      public Builder setTimeToHide(int timeToHide) {
       this.timeToHide = timeToHide;
       return this;
      }
    
      public boolean isAutoHide() {
       return autoHide;
      }
    
      public Builder setAutoHide(boolean autoHide) {
       this.autoHide = autoHide;
       return this;
      }
    
      public Context getContext() {
       return context;
      }
    
      public Builder setActivity(Context context) {
       this.context = context;
       return this;
      }
    
      public Builder(Context context) {
       this.context = context;
      }
      public void setCancelable(boolean flag) {
       throw new RuntimeException("Stub!");
      }
    
      public int getPositiveTextColor() {
       return positiveTextColor;
      }
    
      public Builder setPositiveColor(int positiveTextColor) {
       this.positiveTextColor = positiveTextColor;
       return this;
      }
    
    
      public int getBackgroundColor() {
       return backgroundColor;
      }
    
      public Builder setBackgroundColor(int backgroundColor) {
       this.backgroundColor = backgroundColor;
       return this;
      }
    
      public int getNegativeColor() {
       return negativeColor;
      }
    
      public Builder setNegativeColor(int negativeColor) {
       this.negativeColor = negativeColor;
       return this;
      }
    
    
      public int getTitleColor() {
       return titleColor;
      }
    
      public Builder setTitleColor(int titleColor) {
       this.titleColor = titleColor;
       return this;
      }
    
      public int getBodyColor() {
       return bodyColor;
      }
    
      public Builder setBodyColor(int bodyColor) {
       this.bodyColor = bodyColor;
       return this;
      }
    
      public String getPositiveButtonText() {
       return positiveButtonText;
      }
    
    
      public Builder setPositiveButtonText(int positiveButtonText) {
       this.positiveButtonText = context.getString(positiveButtonText);
       return this;
      }
    
      public Builder setPositiveButtonText(String positiveButtonText) {
       this.positiveButtonText = positiveButtonText;
       return this;
      }
    
      public String getNegativeButtonText() {
       return negativeButtonText;
      }
    
      public Builder setNegativeButtonText(String negativeButtonText) {
       this.negativeButtonText = negativeButtonText;
       return this;
      }
    
      public Builder setNegativeButtonText(int negativeButtonText) {
       this.negativeButtonText = context.getString(negativeButtonText);
       return this;
      }
    
      public String getTextTitle() {
       return textTitle;
      }
    
      public Builder setTextTitle(String textTitle) {
       this.textTitle = textTitle;
       return this;
      }
    
      public Builder setTextTitle(int textTitle) {
       this.textTitle = context.getString(textTitle);
       return this;
      }
    
      public String getBody() {
       return body;
      }
    
      public Builder setBody(String body) {
       this.body = body;
       return this;
      }
    
      public Builder setBody(int body) {
       this.body = context.getString(body);
       return this;
      }
    
      public OnPositiveClicked getOnPositiveClicked() {
       return onPositiveClicked;
      }
    
      public Builder setOnPositiveClicked(OnPositiveClicked onPositiveClicked) {
       this.onPositiveClicked = onPositiveClicked;
       return this;
      }
    
      public OnNegativeClicked getOnNegativeClicked() {
       return onNegativeClicked;
      }
    
      public Builder setOnNegativeClicked(OnNegativeClicked onNegativeClicked) {
       this.onNegativeClicked = onNegativeClicked;
       return this;
      }
    
    
      public Builder build() {
       return this;
      }
    
    
      public Dialog show() {
       return XyAlertDialog.getInstance().show(((Activity) context), this);
      }
    
     }
    
    
     @Override
     public void onPause() {
      if (isAdded() && getActivity() != null) {
       builder = null;
      }
      super.onPause();
     }
    
     public interface OnPositiveClicked {
      void OnClick(View view, Dialog dialog);
     }
    
     public interface OnNegativeClicked {
      void OnClick(View view, Dialog dialog);
     }
    
     public enum PanelGravity {
      LEFT,
      RIGHT,
      CENTER
     }
    //EditText   
     @Override
     public void inputComplete() {
      if (!editText.getEditContent().equals("1234")) {
       text.setText("       ");
       text.setTextColor(Color.RED);
      }
     }
    
     @Override
     public void deleteContent(boolean isDelete) {
      if (isDelete) {
       text.setText("         《    》");
       text.setTextColor(Color.BLACK);
      }
     }
    }
    그 사용자 정의 Countdown Timer 에 대해 서 소개 해 드 리 겠 습 니 다.
    안 드 로 이 드 는 클릭 하여 인증 코드 를 받 은 후 60 초 후에 다시 기능 을 가 져 옵 니 다.
    원본 주소:Android 사용자 정의 사각형 EditText 등록 인증 코드
    이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

    좋은 웹페이지 즐겨찾기