当前位置:网站首页>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(); } }); } }
边栏推荐
- Conditions for JVM to trigger minor GC
- The problem of the sum of leetcode three numbers
- Pat grade a a1076 forwards on Weibo
- 2022 chemical automation control instrument operation certificate test question simulation test platform operation
- What is asynchronous operation
- 2B和2C
- JVM command induction
- The essence of attack and defense strategy behind the noun of network security
- Laravel框架日志文件存放在哪里?怎么用?
- STM32+MFRC522完成IC卡号读取、密码修改、数据读写
猜你喜欢

【Flutter -- 布局】Align、Center、Padding 使用详解

redis原理和使用-安装和分布式配置

js中树与数组的相互转化(树的子节点若为空隐藏children字段)

What are CSDN spaces represented by

Your login IP is not within the login mask configured by the administrator

【Mysql】一条SQL语句是怎么执行的(二)

volatile 靠的是MESI协议解决可见性问题?(上)

Personality test system V1.0

2022 Shanghai safety officer C certificate examination questions and mock examination

Li Mu D2L (V) -- multilayer perceptron
随机推荐
PHP和MySQL获取week值不一致的处理
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
Selection and practice of distributed tracking system
"Could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32"
"No input file specified" problem handling
语音聊天app源码——钠斯直播系统源码
(2006, MySQL server has gone away) problem handling
Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
Innovus is stuck, prompting x error:
839. 模拟堆
Processing of inconsistent week values obtained by PHP and MySQL
JS closure: binding of functions to their lexical environment
HBuilderX 运行微信开发者工具 “Fail to open IDE“报错解决
Pat grade a A1034 head of a gang
756. 蛇形矩阵
NTT(快速数论变换)多项式求逆 一千五百字解析
(2006,Mysql Server has gone away)问题处理
垂直搜索
839. Simulation reactor
OnTap 9 file system limitations