Android常用代码和插件 持续更新~~

总结一下常用的插件和资料,方便以后查阅。

目录

1.Android studio常用插件

1.1 通过Json快速生成Model

1.2 注释模板

2.Android常用代码

2.1 无线调试

2.2 修改Edittext光标

2.3 禁用过度滑动圆弧(例如ScrollView和Recyclerview)

2.4 Android5.0点击水波纹效果(常用)

2.5 隐藏软键盘

2.6 Android 9.0/P 允许明文请求

2.7 ImageView的scaleType属性

2.8 阿里云的Maven镜像仓库

2.9 查询签名信息(微信等一些平台用到)

2.10 gradle操作符

2.11 ScrollView中的控件设置match_parent无效

3.其他常用资料

3.1 第三方库最新版本和地址查询(重要)

3.2 git清空忽略缓存

3.3 各种ui相关第三方库整理(常用)

3.4 Win10中ctrl+空格 快捷键冲突

3.5 git切换域名


 

1.Android studio常用插件

1.1 通过Json快速生成Model

java版:GsonFormat

kotlin版:JsonToKotlinClass

kotlin版本使用时需要设置一下:

按alt+k唤出弹窗之后点击setting

这个一定要改为var,否则你的数据永远都是默认值。(曾经纠结了我半天的神奇问题。)

 

1.2 注释模板

1.类注释模板修改在如图所示位置:

属性在下面有注释,可以自行添加。

 

2.方法注释模板

java的话有JavaDoc这个插件,Kotlin的话暂时没有,如果有相关插件的话跪求告知。

而且Kotlin提倡直接在描述中添加[参数]参数描述,代码如下:

/**
 * Returns the absolute value of the given number.
 * @param number The number to return the absolute value for.
 * @return The absolute value.
 */
fun abs(number: Int) = ...

// Do this instead:

/**
 * Returns the absolute value of the given [number].
 */
fun abs(number: Int) = ...

Kotlin推荐使用如上第二种注释。大家可以习惯下。

 

3.代码模板

如图找到Live Templates,这里有自带的很多代码模板。比如下面这个Toast。在代码中打出“Toast”+“Tab”键就会自动生成代码。

 

4.其他常用代码模板自定义:

推荐:https://github.com/kirtan403/android-studio-live-templates 这里有别人写好的常用代码模板,可以直接拷进如下文件夹内,然后重启studio就行了:

 

 

2.Android常用代码

2.1 无线调试

adb tcpip 8888

adb connect 192.168.3.186:8888(这里填自己手机的IP地址,不行的话端口号换成5555)

断开连接:

adb disconnect

adb disconnect 192.168.3.186:8888(这里填自己手机的IP地址,不行的话端口号换成5555)

 

2.2 修改Edittext光标

android:textCursorDrawable="@drawable/cursor_text" (随意画一个宽1px的线就行)

 

2.3 禁用过度滑动圆弧(例如ScrollView和Recyclerview)

android:overScrollMode="never"

android:scrollbars="none"//不要滚动条

 

2.4 Android5.0点击水波纹效果(常用)

android:background="?attr/selectableItemBackgroundBorderless" //不受边框限制

android:background="?android:attr/selectableItemBackground" //受边框限制

 

2.5 隐藏软键盘

kotlin代码:

    override fun onBackPressed() {//将用到finish()的地方换成此方法
        hintKeyBoard()
        super.onBackPressed()
    }

    private fun hintKeyBoard() {
        //拿到InputMethodManager
        var imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        //如果window上view获取焦点 && view不为空
        if (imm.isActive && currentFocus != null) {
            //拿到view的token 不为空
            if (currentFocus.windowToken != null) {
                //表示软键盘窗口总是隐藏,除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(currentFocus.windowToken, InputMethodManager.HIDE_NOT_ALWAYS); }
        }
    }

java代码:

    @Override
    public void onBackPressed() {//将用到finish()的地方换成此方法
        hintKeyBoard();
        super.onBackPressed();
    }

    private void hintKeyBoard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && getCurrentFocus() != null) {
            //拿到view的token 不为空
            if (getCurrentFocus().getWindowToken() != null) {
                //表示软键盘窗口总是隐藏,除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

 

2.6 Android 9.0/P 允许明文请求

简单说,android 9.0以上需要用https请求,如果用http的话会报错。解决方法如下:

在xml文件夹新建一个network_security_config.xml 

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <!--9.0安全请求问题-->
    <base-config cleartextTrafficPermitted="true">//允许明文传输
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

然后在AndroidManifest.xml中添加:

    <application
        ...
        android:networkSecurityConfig="@xml/network_security_config"

 

2.7 ImageView的scaleType属性

经常用到但老是记不清,从经常看的博客转来一张图:(转自:https://blog.csdn.net/qq_34902522/article/details/76682293

è¿éåå¾çæè¿°

 

2.8 阿里云的Maven镜像仓库

maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }

 

2.9 查询签名信息(微信等一些平台用到)

先切换到jdk的bin路径下,然后输入:

D:\DevelopTools\Java\jdk1.8.0_211\bin>keytool -list -v -keystore D:\xxx\xxx\key.jks(这里填路径)

 

2.10 gradle操作符

gradle时报错时有时候需要看日志,就要用到gradle操作符,常用的如下:

gradlew compileDebugSource --stacktrace -info

gradlew compileDebugSource --stacktrace -debug

gradlew compileDebugSource --stacktrace -stacktrace

(gradlew是Gradle Wrapper的简拼;Gradle Wrapper是android上对gradle的一层包装,直接用gradlew就行)

 

2.11 ScrollView中的控件设置match_parent无效

在ScrollView中添加android:fillViewport="true"即可

 

3.其他常用资料

3.1 第三方库最新版本和地址查询(重要)

http://www.mvnrepository.com/

 

3.2 git清空忽略缓存

git rm -r --cached .

git add .

git commit -m 'update .gitignore'

 

3.3 各种ui相关第三方库整理(常用)

https://hndeveloper.github.io/2017/github-android-ui.html#%E5%9B%BE%E8%A1%A8(Chart

 

3.4 Win10中ctrl+空格 快捷键冲突

在各种编辑器中经常要用到  ctrl+空格  这个快捷键,但是win10中这个快捷键改起来极其麻烦。下面是我找了各种资料找到的有效方法:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Control Panel\Input Method\Hot Keys\00000010]
"Key Modifiers"=hex:00,c0,00,00
"Virtual Key"=hex:ff,00,00,00

[HKEY_CURRENT_USER\Control Panel\Input Method\Hot Keys\00000070]
"Key Modifiers"=hex:00,c0,00,00
"Virtual Key"=hex:ff,00,00,00

[HKEY_USERS\.DEFAULT\Control Panel\Input Method\Hot Keys\00000010]
"Key Modifiers"=hex:02,c0,00,00
"Target IME"=hex:00,00,00,00
"Virtual Key"=hex:ff,00,00,00

[HKEY_USERS\.DEFAULT\Control Panel\Input Method\Hot Keys\00000070]
"Key Modifiers"=hex:02,c0,00,00
"Target IME"=hex:00,00,00,00
"Virtual Key"=hex:ff,00,00,00

新建一个文本将上面复制进去,然后将后缀改为 .reg 然后点击运行。OK,大功告成。

 

3.5 git切换域名

切换到工作空间里,运行:

git remote set-url origin 新路径

更改完成之后可以通过下面命令查询

git remote show origin

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值