当前位置:网站首页>自定义密码输入框,无圆角
自定义密码输入框,无圆角
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(); } }); } }
边栏推荐
- Li Mu D2L (IV) -- softmax regression
- 220. Presence of repeating element III
- "No input file specified" problem handling
- Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
- 深度学习常用激活函数总结
- Pat grade a A1034 head of a gang
- NTT (fast number theory transformation) polynomial inverse 1500 word analysis
- Canal 的学习笔记
- C# Serialport的发送和接收
- 您的登录IP不在管理员配置的登录掩码范围内
猜你喜欢
随机推荐
Tornado multi process service
Windows通过命令备份数据库到本地
The essence of attack and defense strategy behind the noun of network security
2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
NFT与数字藏品到底有何区别?
760. String length
聪明的美食家 C语言
Hbuilderx runs the wechat developer tool "fail to open ide" to solve the error
Canal 的学习笔记
Pat grade a A1034 head of a gang
公告 | FISCO BCOS v3.0-rc4发布,新增Max版,可支撑海量交易上链
760. 字符串长度
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
2022 mobile crane driver test question simulation test question bank simulation test platform operation
LeetCode三数之和问题
220. Presence of repeating element III
JS closure: binding of functions to their lexical environment
PHP和MySQL获取week值不一致的处理
Vertical search
Original root and NTT 5000 word explanation









