iT邦幫忙

0

每日一篇學習筆記 直到我做完專題 :( [Day16]

  • 分享至 

  • xImage
  •  

今天學edittext的焦點變更監聽器和文本變更監聽器

利用設置監聽焦點變更來確認輸入的電話號碼是不是正確的形式

public class EditFocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {

    private EditText et_phone;
    private EditText et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_edit_focus);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        et_phone = findViewById(R.id.et_phone);
        et_password = findViewById(R.id.et_password);
        et_password.setOnFocusChangeListener(this);
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if(hasWindowFocus()){
            String phonenum = et_phone.getText().toString();
            if(TextUtils.isEmpty(phonenum)||phonenum.length()<10 ){
                et_phone.requestFocus();
                Toast.makeText(this,"請輸入正確的手機號碼",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

利用設置文本監聽器使輸入到正確的密碼數量時 把鍵盤關掉

public class EditHideActivity extends AppCompatActivity {

    private EditText et_password;
    private EditText et_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_edit_hide);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        et_phone = findViewById(R.id.et_phone);
        et_password = findViewById(R.id.et_password);

        et_phone.addTextChangedListener(new HideTextWatcher(et_phone,10));
        et_password.addTextChangedListener(new HideTextWatcher(et_password,6));
    }

    private class HideTextWatcher implements TextWatcher {

        private EditText mView;
        private int mMaxLength;

        public HideTextWatcher(EditText etPhone, int maxLength) {
            this.mView = etPhone;
            this.mMaxLength = maxLength;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String str = editable.toString();
            if(str.length() == mMaxLength){
                ViewUtil.hiadeOneInputMethod(EditHideActivity.this,mView);
            }
        }
    }
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言