当前位置:网站首页>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(); } }); } }
边栏推荐
猜你喜欢
Object 的Wait Notify NotifyAll 源码解析
MySQL transaction
Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
Vertical search
Your login IP is not within the login mask configured by the administrator
2022 mobile crane driver test question simulation test question bank simulation test platform operation
Li Mu D2L (VI) -- model selection
语音聊天app源码——钠斯直播系统源码
CF1481C Fence Painting
随机推荐
Innovus is stuck, prompting x error:
"No input file specified" problem handling
Vertical search
PHP和MySQL获取week值不一致的处理
Server memory failure prediction can actually do this!
Basic use of ArcGIS 1
JVM触发minor gc的条件
js中树与数组的相互转化(树的子节点若为空隐藏children字段)
Conditions for JVM to trigger minor GC
VectorTileLayer更换style
(2006,Mysql Server has gone away)问题处理
Two tips for pycharm to open multiple projects
Study notes of dataX
【Mysql】Mysql锁详解(三)
Windows backs up the database locally by command
Codeworks DP collection
volatile 靠的是MESI协议解决可见性问题?(下)
Qtcreator reports an error: you need to set an executable in the custom run configuration
Tornado multi process service
Paper notes: knowledge map kgat (unfinished temporary storage)