Android实践中容易忘的小点

188 阅读1分钟

1、editText 默认密文

android:inputType="textPassword"

2、editText 明密文切换

我一般是 editText + checkBox,监听checkbox实现明密文切换

 private void setChecked(boolean isChecked, EditText passwordInput) {
        if (isChecked) {
            //显示明文
            passwordInput.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            passwordInput.setSelection(passwordInput.getText().toString().length());
        } else {
            //显示密文
            passwordInput.setTransformationMethod(PasswordTransformationMethod.getInstance());
            passwordInput.setSelection(passwordInput.getText().toString().length());
        }
    }

3、输入3-4-4的手机号码

    @OnTextChanged(value = R.id.mobileInput, callback = OnTextChanged.Callback.BEFORE_TEXT_CHANGED)
    void beforeMobileTextChanged(CharSequence s, int start, int count, int after) {
        mPreviousMobileLength = s.length();
    }

    @OnTextChanged(value = R.id.mobileInput, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void afterMobileTextChanged(Editable s) {
        int mobileLength = s.length();
        if (mPreviousMobileLength > mobileLength) {
            //删除操作
            loginBtn.setSelected(false);
            if (mobileLength == 4 || mobileLength == 9) {
                mobileInput.setText(mobileInput.getText().subSequence(0, mobileLength - 1));
                mobileInput.setSelection(mobileLength - 1);
            }
        } else {
            if (mobileLength == 3 || mobileLength == 8) {
                mobileInput.setText(mobileInput.getText().toString() + " ");
                mobileInput.setSelection(mobileLength + 1);
            }
        }
    }

4、倒计时效果

 private void countDown(int count) {
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(aLong -> count - aLong)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        countDownDisposable = d;
                        verifyCodeSend.setVisibility(View.VISIBLE);
                        verifyCodeSend.setTextColor(Color.parseColor("#CCCCCC"));
                        verifyCodeSend.setEnabled(false);
                    }

                    @Override
                    public void onNext(Long aLong) {
                        verifyCodeSend.setText(aLong + "s");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        shutDownCountDownDisposable();
                    }
                });
    }

    private void shutDownCountDownDisposable() {
        if (countDownDisposable != null && !countDownDisposable.isDisposed()) {
            Log.e("zhen", "shutDownCountDownDisposable");
            countDownDisposable.dispose();
            verifyCodeSend.setEnabled(true);
            verifyCodeSend.setText("获取验证码");
            verifyCodeSend.setTextColor(colorRed);
        }
    }

5、checkBox自定义样式实现方式

 <CheckBox
            android:id="@+id/passwordVisible"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:button="@drawable/sel_password_visible_or_not"
            app:layout_constraintRight_toRightOf="parent" />

横竖屏切换不重新走生命周期

  1. 屏幕不旋转 在AndroidManifest文件中的对应Activity中配置android:screenOrientation=”landscape”(横屏,portrait是竖屏);
  2. 屏幕旋转时不重新走生命周期 在AndroidManifest文件中的对应Activity中配置android:configChanges="keyboardHidden|orientation|screenSize",