android 实现自定义密码框,输入密码,显示对应圆点填充代码
时间: 2023-04-01 10:04:03 浏览: 457
您好,以下是实现自定义密码框的代码:
1. 在 XML 布局文件中添加一个 EditText 控件,设置输入类型为密码:
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
2. 创建一个自定义的 PasswordView 类,继承自 View,用于绘制密码框:
public class PasswordView extends View {
private Paint mPaint;
private int mWidth;
private int mHeight;
private int mCircleRadius;
private int mCircleSpacing;
private int mCircleCount;
private int mPasswordLength;
private int[] mPassword;
public PasswordView(Context context) {
this(context, null);
}
public PasswordView(Context context, AttributeSet attrs) {
this(context, attrs, );
}
public PasswordView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.BLACK);
mCircleRadius = dp2px(10);
mCircleSpacing = dp2px(20);
mCircleCount = 6;
mPasswordLength = ;
mPassword = new int[mCircleCount];
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int startX = (mWidth - mCircleRadius * 2 * mCircleCount - mCircleSpacing * (mCircleCount - 1)) / 2;
int centerY = mHeight / 2;
for (int i = ; i < mCircleCount; i++) {
int x = startX + i * (mCircleRadius * 2 + mCircleSpacing);
canvas.drawCircle(x, centerY, mCircleRadius, mPaint);
if (i < mPasswordLength) {
canvas.drawCircle(x, centerY, mCircleRadius - dp2px(2), mPaint);
}
}
}
public void setPassword(int[] password) {
mPassword = password;
mPasswordLength = password.length;
invalidate();
}
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
}
3. 在 Activity 中,获取 EditText 控件和自定义的 PasswordView 控件,并设置监听器,当 EditText 的文本发生变化时,更新 PasswordView 的密码显示:
public class MainActivity extends AppCompatActivity {
private EditText mPasswordEditText;
private PasswordView mPasswordView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPasswordEditText = findViewById(R.id.password_edittext);
mPasswordView = findViewById(R.id.password_view);
mPasswordEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String passwordStr = s.toString();
int[] password = new int[passwordStr.length()];
for (int i = ; i < passwordStr.length(); i++) {
password[i] = Integer.parseInt(String.valueOf(passwordStr.charAt(i)));
}
mPasswordView.setPassword(password);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
希望能对您有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)