当前位置:网站首页>自定义密码输入框,无圆角
自定义密码输入框,无圆角
2022-07-26 09:19:00 【LHBTM】
点击按钮,显示一个自定义的密码输入框.点击输入框,调用系统键盘,密码输入完后,拿到密码做相应的逻辑
布局文件
<?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"> <!-- 取消按钮 --> <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="输入密码" android:textColor="#898181" android:textSize="20sp" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#555555" /> <!-- 6位密码框布局,需要一个圆角边框的shape作为layout的背景 --> <!--<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>--> <!-- 忘记密码链接 --> <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="忘记密码?" android:textColor="#354EEF" /> </LinearLayout> </RelativeLayout>
自定义的密码输入框工具类
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; /** * 自定义支付密码输入框 * Created by Administrator on 2016/12/27 0027. */ public class PwdEditText extends EditText { /** * 间隔 */ private final int PWD_SPACING = 0; /** * 密码大小 */ private final int PWD_SIZE = 15; /** * 密码长度 */ private final int PWD_LENGTH = 6; /** * 上下文 */ private Context mContext; /** * 宽度 */ private int mWidth; /** * 高度 */ private int mHeight; /** * 密码框 */ private Rect mRect; /** * 密码画笔 */ private Paint mPwdPaint; /** * 密码框画笔 */ private Paint mRectPaint; /** * 输入的密码长度 */ private int mInputLength; /** * 输入结束监听 */ private OnInputFinishListener mOnInputFinishListener; /** * 构造方法 * * @param context * @param attrs */ public PwdEditText(Context context, AttributeSet attrs) { super(context, attrs); // 初始化密码画笔 mPwdPaint = new Paint(); mPwdPaint.setColor(Color.BLACK);//输入密码的颜色,画笔颜色 mPwdPaint.setStyle(Paint.Style.FILL);//画笔类型 ,fill为实心 mPwdPaint.setAntiAlias(true);//消除密码圆点锯齿,但实际效果没有太大变化 // 初始化密码框 mRectPaint = new Paint(); mRectPaint.setStyle(Paint.Style.STROKE);//画笔类型 ,fill为空心 mRectPaint.setColor(Color.GRAY);//密码框的颜色 mRectPaint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mWidth = getWidth(); mHeight = getHeight(); // 这三行代码非常关键,大家可以注释点在看看效果 Paint paint = new Paint(); paint.setColor(Color.WHITE);//整个区域内的颜色会发生改变 canvas.drawRect(0, 0, mWidth, mHeight, paint); // 计算每个密码框宽度 int rectWidth = (mWidth - PWD_SPACING * (PWD_LENGTH - 1)) / PWD_LENGTH; // 绘制密码框 for (int i = 0; i < PWD_LENGTH; i++) { int left = (rectWidth + PWD_SPACING) * i;//距离左边的高度。就好像xml界面布局中paddleft一样 int top =2;//距离上边的高度。就好像xml界面布局中paddTop一样 int right = left + rectWidth;//距离右边的高度。就好像xml界面布局中paddright一样 int bottom = mHeight - top;//距离下边的高度。就好像xml界面布局中paddbottom一样 mRect = new Rect(left, top, right, bottom); canvas.drawRect(mRect, mRectPaint); } // 绘制密码 for (int i = 0; i < mInputLength; i++) { int cx = rectWidth / 2 + (rectWidth + PWD_SPACING) * i;//密码圆点到密码框内的距离 除以2相当于有xml中的center居中效果 int cy = mHeight / 2; canvas.drawCircle(cx, cy, PWD_SIZE, mPwdPaint); } } //关键部分 此类在被别的类调用时 会直接将输入的内容返回到调用者去 ,重写了onTextChanged方法 @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 (mInputLength == PWD_LENGTH && mOnInputFinishListener != null) { mOnInputFinishListener.onInputFinish(text.toString()); Log.d("*****************",text.toString()); //text就是输入的字符 ,通过toString转换为字符串 } } public interface OnInputFinishListener { /** * 密码输入结束监听 * * @param password */ void onInputFinish(String password); } /** * 设置输入完成监听,此方法被activity来调用 * * @param onInputFinishListener */ public void setOnInputFinishListener( OnInputFinishListener onInputFinishListener) { this.mOnInputFinishListener = onInputFinishListener; } /** * 设置晃动动画 */ public void setShakeAnimation() { this.startAnimation(shakeAnimation(5)); } /** * 晃动动画 * * @param counts * 1秒钟晃动多少下 * @return */ public static Animation shakeAnimation(int counts) { Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); //设置一个循环加速器,使用传入的次数就会出现摆动的效果。 translateAnimation.setInterpolator(new CycleInterpolator(counts)); translateAnimation.setDuration(500); return translateAnimation; } }
Mainactivity实现一个按钮,点击
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); //对dialog的范围进行规范 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,"输入结束" + 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(); } }); } }
边栏推荐
- CF1481C Fence Painting
- 对标注文件夹进行清洗
- 李沐d2l(五)---多层感知机
- Where are the laravel framework log files stored? How to use it?
- Local cache
- Use of off heap memory
- How to quickly learn a programming language
- Sending and receiving of C serialport
- OnTap 9 file system limitations
- 【线上问题】Timeout waiting for connection from pool 问题排查
猜你喜欢

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

您的登录IP不在管理员配置的登录掩码范围内

Zipkin安装和使用

The Child and Binary Tree-多项式开根求逆

Redis principle and usage - installation and distributed configuration

220. Presence of repeating element III

【线上死锁分析】由index_merge引发的死锁事件

Use of off heap memory

Zipkin installation and use

李沐d2l(六)---模型选择
随机推荐
pycharm 打开多个项目的两种小技巧
MySQL 强化知识点
2022 chemical automation control instrument operation certificate test question simulation test platform operation
Hbuilderx runs the wechat developer tool "fail to open ide" to solve the error
JVM触发minor gc的条件
volatile 靠的是MESI协议解决可见性问题?(下)
Selection and practice of distributed tracking system
多项式开根
李沐d2l(六)---模型选择
Li Mu D2L (V) -- multilayer perceptron
220. Presence of repeating element III
(2006, MySQL server has gone away) problem handling
QT | about how to use EventFilter
PAT 甲级 A1034 Head of a Gang
The essence of attack and defense strategy behind the noun of network security
tornado之多进程服务
Clean the label folder
jvm命令归纳
js在控制台输出菱形
ext3文件系统的一个目录下,无法创建子文件夹,但可以创建文件