当前位置:网站首页>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(); } }); } }
边栏推荐
猜你喜欢

Redis principle and usage - installation and distributed configuration

原根与NTT 五千字详解

垂直搜索
![[MySQL] how to execute an SQL statement (2)](/img/7b/53f8756458cc318e2f417b1cc0c3f8.png)
[MySQL] how to execute an SQL statement (2)

2022 mobile crane driver test question simulation test question bank simulation test platform operation

Grain College of all learning source code
![[MySQL] detailed explanation of redo log, undo log and binlog (4)](/img/67/6e646040c1b941c270b3efff74e94d.png)
[MySQL] detailed explanation of redo log, undo log and binlog (4)

2022 Shanghai safety officer C certificate examination questions and mock examination

Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points

2022 chemical automation control instrument operation certificate test question simulation test platform operation
随机推荐
多项式开根
【Mysql】一条SQL语句是怎么执行的(二)
大二上第三周学习笔记
“No input file specified “问题的处理
Processing of inconsistent week values obtained by PHP and MySQL
Grain College of all learning source code
2B和2C
Use of off heap memory
When you click input, the border is not displayed!
839. Simulation reactor
Redis principle and use - Basic Features
Li Mu D2L (IV) -- softmax regression
What are CSDN spaces represented by
Sliding window, double pointer, monotone queue, monotone stack
Pat grade a a1013 battle over cities
对象型的集合按某个属性的值进行去重
HBuilderX 运行微信开发者工具 “Fail to open IDE“报错解决
Selection and practice of distributed tracking system
Error: Cannot find module ‘umi‘ 问题处理
设置视图动态图片