PasswordDialog.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.ht.gate;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.text.TextUtils;
  6. import android.util.TypedValue;
  7. import android.view.Gravity;
  8. import android.view.View;
  9. import android.view.Window;
  10. import android.view.WindowManager;
  11. import android.widget.TextView;
  12. import androidx.appcompat.app.AppCompatDialog;
  13. import butterknife.BindView;
  14. import butterknife.ButterKnife;
  15. public class PasswordDialog extends AppCompatDialog {
  16. private final int MSG_HIDE_PASSWORD = 1;
  17. public interface DialogListener {
  18. void onCancel(PasswordDialog dialog);
  19. void onConfirm(PasswordDialog dialog, String password);
  20. }
  21. @BindView(R.id.keyboard_view)
  22. CustomKeyboardView keyboardView;
  23. @BindView(R.id.tv_room_name)
  24. TextView tv1;
  25. @BindView(R.id.tv_space_name)
  26. TextView tv2;
  27. @BindView(R.id.tv3)
  28. TextView tv3;
  29. @BindView(R.id.tv4)
  30. TextView tv4;
  31. private TextView[] textViews;
  32. private String password = "";
  33. private String passwordStr = "";
  34. private DialogListener listener;
  35. private Handler mHandler = new Handler(msg -> {
  36. switch (msg.what) {
  37. case MSG_HIDE_PASSWORD:
  38. passwordStr = passwordStr.replaceAll(".", "●");
  39. updatePasswordTv();
  40. break;
  41. }
  42. return true;
  43. });
  44. public PasswordDialog(Context context) {
  45. super(context, R.style.DialogStyle);
  46. }
  47. public void setListener(DialogListener listener) {
  48. this.listener = listener;
  49. }
  50. @Override
  51. protected void onCreate(Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. setContentView(R.layout.dialog_password);
  54. ButterKnife.bind(this);
  55. Window window = getWindow();
  56. WindowManager.LayoutParams wlp = window.getAttributes();
  57. wlp.gravity = Gravity.BOTTOM;
  58. window.setAttributes(wlp);
  59. window.setWindowAnimations(R.style.DialogAnimation);
  60. window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
  61. View decorView = getWindow().getDecorView();
  62. int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  63. | View.SYSTEM_UI_FLAG_FULLSCREEN
  64. | View.SYSTEM_UI_FLAG_IMMERSIVE;
  65. decorView.setSystemUiVisibility(uiOptions);
  66. textViews = new TextView[]{tv1, tv2, tv3, tv4};
  67. keyboardView.setKeyboardActionListener(new CustomKeyboardView.OnKeyboardActionListener() {
  68. @Override
  69. public void onPress(int primaryCode) {
  70. }
  71. @Override
  72. public void onRelease(int primaryCode) {
  73. }
  74. @Override
  75. public void onKey(int primaryCode, int[] keyCodes) {
  76. switch (primaryCode) {
  77. case CustomKeyboardView.KEYCODE_CANCEL:
  78. if (listener != null) {
  79. listener.onCancel(PasswordDialog.this);
  80. }
  81. break;
  82. case CustomKeyboardView.KEYCODE_DONE:
  83. if (listener != null) {
  84. listener.onConfirm(PasswordDialog.this, password);
  85. }
  86. break;
  87. case CustomKeyboardView.KEYCODE_DELETE:
  88. if (password.length() > 0) {
  89. password = password.substring(0, password.length() - 1);
  90. passwordStr = password.replaceAll(".", "●");
  91. updatePasswordTv();
  92. }
  93. break;
  94. default:
  95. String str = Character.toString((char) primaryCode);
  96. if (!TextUtils.isEmpty(str) && password.length() < 4) {
  97. str = str.substring(0, 1);
  98. password += str;
  99. passwordStr = password.substring(0, password.length() - 1).replaceAll(".", "●") + str;
  100. updatePasswordTv();
  101. }
  102. mHandler.removeMessages(MSG_HIDE_PASSWORD);
  103. mHandler.sendEmptyMessageDelayed(MSG_HIDE_PASSWORD, 1000);
  104. if (password.length() == 4) {
  105. if (listener != null) {
  106. listener.onConfirm(PasswordDialog.this, password);
  107. }
  108. }
  109. break;
  110. }
  111. }
  112. @Override
  113. public void onText(CharSequence text) {
  114. }
  115. });
  116. }
  117. private void updatePasswordTv() {
  118. for (int i = 0; i < textViews.length; i++) {
  119. if (passwordStr.length() > i) {
  120. textViews[i].setText(passwordStr.charAt(i) + "");
  121. } else {
  122. textViews[i].setText("");
  123. }
  124. if (textViews[i].getText().toString().equals("●")) {
  125. textViews[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
  126. } else {
  127. textViews[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 37);
  128. }
  129. }
  130. }
  131. }