android常用代码

输入框先不获取焦点,在外层使用

android:focusable="true"
android:focusableInTouchMode="true"

点击屏幕隐藏键盘

    private InputMethodManager mInputMethodManager;
        mInputMethodManager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
    protected void hideSoftKeyBoard() {
        View localView = getCurrentFocus();
        if (mInputMethodManager == null) {
            mInputMethodManager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        }
        if ((localView != null) && (mInputMethodManager != null)) {
            mInputMethodManager.hideSoftInputFromWindow(localView.getWindowToken(), 2);
        }
    }

    protected boolean isNeedHideKeyboard() {
        return true;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (isNeedHideKeyboard()) {
            if (event.getAction() == MotionEvent.ACTION_DOWN && mInputMethodManager.isActive()) {
                View v = getCurrentFocus();
                if (isShouldHideKeyboard(v, event)) {
                    hideSoftKeyBoard();
                }
            }
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,
     * 因为当用户点击EditText时则不能隐藏
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v instanceof EditText) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getY() > top-20 && event.getY() < bottom + 20) { // event.getX() > left && event.getX() < right &&
                // 点击EditText的事件,忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditText上,和用户用轨迹球选择其他的焦点
        return false;
    }

在输入框变化时判断文字再设置文字,这样会造成死循环,可用移除后再添加

        mEditText.removeTextChangedListener(textWatcher);
        mEditText.setText(keyword);
        mEditText.addTextChangedListener(textWatcher);

打开qq聊天

val url = "mqqwpa://im/chat?chat_type=wpa&uin=$linkQQ"
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))

锁定app字体大小,不随系统改变

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics());
        return res;
    }

apk打包签名pkcs12

keytool -importkeystore -srckeystore C:...\key.jks -destkeystore C:...\key.jks -deststoretype pkcs12

查看手机中的日志

adb logcat -v time > d:/logcat.log

打jar包

task makeJar(type: Copy) {
    //删除存在的
    delete 'build/jar/myjar.jar'
    //设置拷贝的文件
    from('build/intermediates/aar_main_jar/release/')
    //打进jar包后的文件目录
    into('build/jar/')
    //重命名
    rename ('classes.jar', 'myjar.jar')
}

makeJar.dependsOn(build)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值