当前位置:网站首页>Custom password input box, no rounded corners
Custom password input box, no rounded corners
2022-07-26 09:22:00 【LHBTM】
Click button , Display a custom password input box . Click on the input box , Call the system keyboard , After entering the password , Get the password and do the corresponding logic
Layout file
<?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="match_parent" android:background="#FFFFFF" android:gravity="center_vertical"> <LinearLayout android:id="@+id/linear_pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp"> <!-- Cancel button --> <ImageView android:id="@+id/img_cancel" android:layout_width="20sp" android:layout_height="20sp" android:layout_centerVertical="true" android:background="@drawable/delete" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text=" Input password " android:textColor="#898181" android:textSize="20sp" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#555555" /> <!-- 6 Bit password box layout , Need a rounded border shape As layout The background of --> <!--<RelativeLayout--> <!--android:layout_width="match_parent"--> <!--android:layout_height="wrap_content"--> <!--android:layout_marginTop="50dp">--> <com.example.administrator.pwd.PwdEditText android:id="@+id/pwd" android:layout_width="550px" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:layout_gravity="center_horizontal" android:layout_centerHorizontal="true" android:inputType="numberPassword" /> <!--</RelativeLayout>--> <!-- Forget the password link --> <TextView android:id="@+id/tv_forgetPwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_margin="15dp" android:text=" Forget the password ?" android:textColor="#354EEF" /> </LinearLayout> </RelativeLayout>
Custom password input box tool class
package com.example.administrator.pwd; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.util.Log; import android.view.animation.Animation; import android.view.animation.CycleInterpolator; import android.view.animation.TranslateAnimation; import android.widget.EditText; /** * Custom payment password input box * Created by Administrator on 2016/12/27 0027. */ public class PwdEditText extends EditText { /** * interval */ private final int PWD_SPACING = 0; /** * Password size */ private final int PWD_SIZE = 15; /** * Password length */ private final int PWD_LENGTH = 6; /** * Context */ private Context mContext; /** * Width */ private int mWidth; /** * Height */ private int mHeight; /** * Password box */ private Rect mRect; /** * Password brush */ private Paint mPwdPaint; /** * Password box brush */ private Paint mRectPaint; /** * The length of the password entered */ private int mInputLength; /** * Enter to end listening */ private OnInputFinishListener mOnInputFinishListener; /** * Construction method * * @param context * @param attrs */ public PwdEditText(Context context, AttributeSet attrs) { super(context, attrs); // Initialize password brush mPwdPaint = new Paint(); mPwdPaint.setColor(Color.BLACK);// Enter the color of the password , Brush color mPwdPaint.setStyle(Paint.Style.FILL);// Brush type ,fill It's solid mPwdPaint.setAntiAlias(true);// Eliminate password dot aliasing , But the actual effect has not changed much // Initialize the password box mRectPaint = new Paint(); mRectPaint.setStyle(Paint.Style.STROKE);// Brush type ,fill Is hollow mRectPaint.setColor(Color.GRAY);// The color of the password box mRectPaint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mWidth = getWidth(); mHeight = getHeight(); // These three lines of code are critical , You can comment on the effect Paint paint = new Paint(); paint.setColor(Color.WHITE);// The color in the whole area will change canvas.drawRect(0, 0, mWidth, mHeight, paint); // Calculate the width of each password box int rectWidth = (mWidth - PWD_SPACING * (PWD_LENGTH - 1)) / PWD_LENGTH; // Draw password box for (int i = 0; i < PWD_LENGTH; i++) { int left = (rectWidth + PWD_SPACING) * i;// Height to the left . It's like xml In the interface layout paddleft equally int top =2;// Height from the top . It's like xml In the interface layout paddTop equally int right = left + rectWidth;// Height from the right . It's like xml In the interface layout paddright equally int bottom = mHeight - top;// Height from the bottom . It's like xml In the interface layout paddbottom equally mRect = new Rect(left, top, right, bottom); canvas.drawRect(mRect, mRectPaint); } // Draw password for (int i = 0; i < mInputLength; i++) { int cx = rectWidth / 2 + (rectWidth + PWD_SPACING) * i;// The distance from the password dot to the password box Divide 2 Equivalent to xml Medium center Center effect int cy = mHeight / 2; canvas.drawCircle(cx, cy, PWD_SIZE, mPwdPaint); } } // A key part When this class is called by another class The input content will be directly returned to the caller , Rewrote onTextChanged Method @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { super.onTextChanged(text, start, lengthBefore, lengthAfter); this.mInputLength = text.toString().length(); invalidate(); /** * If the input length is equal to the password length Input end answer is not empty */ if (mInputLength == PWD_LENGTH && mOnInputFinishListener != null) { mOnInputFinishListener.onInputFinish(text.toString()); Log.d("*****************",text.toString()); //text Is the character entered , adopt toString Convert to string } } public interface OnInputFinishListener { /** * Password input ends listening * * @param password */ void onInputFinish(String password); } /** * Set input to finish listening , This method is activity To call * * @param onInputFinishListener */ public void setOnInputFinishListener( OnInputFinishListener onInputFinishListener) { this.mOnInputFinishListener = onInputFinishListener; } /** * Animate shaking */ public void setShakeAnimation() { this.startAnimation(shakeAnimation(5)); } /** * Shake animation * * @param counts * 1 How many times per second * @return */ public static Animation shakeAnimation(int counts) { Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); // Set up a cycle accelerator , Using the incoming number of times will have the effect of swinging . translateAnimation.setInterpolator(new CycleInterpolator(counts)); translateAnimation.setDuration(500); return translateAnimation; } }
Mainactivity Implement a button , Click on
package com.example.administrator.pwd; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Toast; /** * Created by Administrator on 2017/2/14 0014. */ public class MineDiaog extends Dialog implements DialogInterface { private final Context context; private PwdEditText bb; private View mDialogView; private PwdEditText pwd; public MineDiaog(Context context) { super(context); this.context = context; } public MineDiaog(Context context, int themeResId, Context context1) { super(context, themeResId); this.context = context1; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Yes dialog The scope of WindowManager.LayoutParams params = getWindow().getAttributes(); getWindow().setBackgroundDrawable(new BitmapDrawable()); params.height = ViewGroup.LayoutParams.WRAP_CONTENT; params.width = ViewGroup.LayoutParams.WRAP_CONTENT; getWindow().setAttributes(params); mDialogView = ViewGroup.inflate(context, R.layout.dialog, null); pwd = (PwdEditText) mDialogView.findViewById(R.id.pwd); setContentView(mDialogView); pwd.setOnInputFinishListener(new PwdEditText.OnInputFinishListener() { @Override public void onInputFinish(String password) { Toast.makeText(context," End of input " + pwd.getText().toString(),Toast.LENGTH_SHORT).show(); pwd.setText(null); } }); } }
MAinActivity
package com.example.administrator.pwd; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import im.quar.autolayout.AutoLayoutActivity; public class MainActivity extends AutoLayoutActivity { private RelativeLayout bb; private PwdEditText pwd; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MineDiaog mineDiaog = new MineDiaog(MainActivity.this); mineDiaog.show(); } }); } }
边栏推荐
猜你喜欢
使用openLayer画箭头
(2006,Mysql Server has gone away)问题处理
Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
【线上问题】Timeout waiting for connection from pool 问题排查
Unity topdown character movement control
Qtcreator reports an error: you need to set an executable in the custom run configuration
arcgis的基本使用1
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
CF1481C Fence Painting
Redis principle and use - Basic Features
随机推荐
Pat grade a a1013 battle over cities
Thread Join 和Object wait 的区别
什么是异步操作
堆外内存的使用
神经网络与深度学习-6- 支持向量机1 -PyTorch
760. 字符串长度
(2006, MySQL server has gone away) problem handling
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
JVM command induction
Original root and NTT 5000 word explanation
解决“NOTE: One or more layouts are missing the layout_width or layout_height attributes.”
Byte buffer stream & character stream explanation
Li Mu D2L (V) -- multilayer perceptron
【Mysql】redo log,undo log 和binlog详解(四)
Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
大二上第二周学习笔记
VS2019配置opencv
JS output diamond on the console
Basic use of ArcGIS 1
安卓 实现缓存机制,多种数据类型缓存