前言
我们在开发过程中有时候会遇到一些比较棘手的问题,经过不懈搜索后发现原来是这么简单,自己却想的异常复杂,这些知识需要归纳整理起来方便大家使用,所以你有什么好的技巧也分享出来吧。
EditText不自动获取焦点
问题描述:有时候EditText会自动获取焦点,导致软键盘直接跳出。有时候这么做很方便,但是大部分时候我们还是希望在点击EditText的时候软键盘才弹出来。
解决方法:在EditText的父Layout中,加入下面的两个属性。
1
2
| android:focusable="true"
android:focusableInTouchMode="true"
|
负dp布局
问题描述:有时候需要做提示小圆点,方式有很多,但是这个方式也是比较简单的一种。
1
2
| android:layout_marginTop="-3dp"
android:layout_marginRight="-3dp"
|
运行时忽略
问题描述:很多时候我们在写xml界面的时候需要预览数据,但是数据又不能在实际运行中被显示出来。
在跟布局中添加:
1
| xmlns:tools="http://schemas.android.com/tools"
|
然后使用 tools:
开头的属性:
或者使用 layout_width
和 layout_height
来约定适配器item的宽度和高度。
1
2
| tools:layout_height="370pt"
tools:layout_width="220pt"
|
还有种情况也很实用,就是使用 showIn
属性将item布局预览展示到某个布局内部。
1
| tools:showIn="@layout/activity_main"
|
LevelDrawable使用
问题描述:有的时候我们需要一个按钮或者一个图片显示多种状态的不同内容(超过两种状态),这个时候可以使用 LevelDrawable 实现。
在 res/drawable
下创建文件level_colors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| <level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/colorAccent"
android:maxLevel="1"
android:minLevel="0" />
<item
android:drawable="@color/colorPrimary"
android:maxLevel="2"
android:minLevel="1" />
<item
android:drawable="@color/colorPrimaryDark"
android:maxLevel="3"
android:minLevel="2" />
</level-list>
|
示例布局:
1
2
3
4
5
6
7
8
9
10
11
12
13
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.chenjc.testtablelayout.MainActivity">
<ImageView
android:id="@+id/image_level"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/level_colors" />
</RelativeLayout>
|
java 代码中设置level来改变不同颜色:
1
2
| ImageView mImageLevel = (ImageView) findViewById(R.id.image_level);
mImageLevel.setImageLevel(3);
|
如果是通过 background 引用的,控件可直接通过 getBackground 方法获取 LevelListDrawable
对象。
使用 ShrinkResources
shrinkResources 是把你没用到的文件用一个很小的文件替换,我觉得要是你发现了那个文件确实没啥用,你还是删除了吧。
1
2
3
4
5
6
7
8
9
10
| android {
...
buildTypes {
release {
shrinkResources true
minifyEnabled true
...
}
}
}
|
将弹出的软键盘的回车键改为搜索键
1
2
3
4
5
6
7
| <EditText
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionSearch"
android:singleLine="true"
android:inputType="text"/>
|
其中 android:imeOptions
需要配合 android:inputType
属性(或者singleLine属性,PS:单独设置maxLines并不能解决问题)才能使回车键变为需要的图标。
1
2
3
4
5
6
7
8
9
10
| etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
initData();
return true;
}
return false;
}
});
|
当界面不可见时释放内存
当用户打开了另外一个程序,我们的程序界面已经不再可见的时候,我们应当将所有和界面相关的资源进行释放。在这种场景下释放资源可以让系统缓存后台进程的能力显著增加,因此也会让用户体验变得更好。
那么我们如何才能知道程序界面是不是已经不可见了呢?其实很简单,只需要在 Activity 中重写 onTrimMemory()
方法,然后在这个方法中监听 TRIM_MEMORY_UI_HIDDEN
这个级别,一旦触发了之后就说明用户已经离开了我们的程序,那么此时就可以进行资源释放操作了,如下所示:
1
2
3
4
5
6
7
8
9
| @Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
switch (level) {
case TRIM_MEMORY_UI_HIDDEN:
// 进行资源释放操作
break;
}
}
|
注意 onTrimMemory()
方法中的 TRIM_MEMORY_UI_HIDDEN
回调只有当我们程序中的所有 UI 组件全部不可见的时候才会触发,这和 onStop() 方法还是有很大区别的,因为 onStop() 方法只是当一个 Activity 完全不可见的时候就会调用,比如说用户打开了我们程序中的另一个 Activity。因此,我们可以在 onStop() 方法中去释放一些 Activity 相关的资源,比如说取消网络连接或者注销广播接收器等,但是像UI相关的资源应该一直要等到 onTrimMemory(TRIM_MEMORY_UI_HIDDEN) 这个回调之后才去释放,这样可以保证如果用户只是从我们程序的一个Activity 回到了另外一个 Activity,界面相关的资源都不需要重新加载,从而提升响应速度。
显示时间控件
问题描述:有时候我们需要显示当前日期或者当前时间(如下图),这个时候如果自己用 TextView 再配合计时器就麻烦很多了。
Android TextClock 是在 Android 4.2 ( API 17 ) 后推出的以字符串格式显示当前的日期和时间的控件。
TextClock 提供了两种不同的格式,一种是在 24 进制中显示时间和日期,另一种是在 12 进制中显示时间和日期。默认是 24 进制,可以通过 is24HourModeEnabled()
方法来查询系统是否在使用 24 进制时间显示。
1
2
3
4
| <TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format24Hour="yyyy 年 MM 月 dd 日 hh 时 mm 分 ss 秒"/>
|
TextClock 控件主要属性和方法:
属性 | 对应方法 | 说明 |
---|
android:format12Hour | setFormat12Hour(CharSequence) | 设置 12 时制的格式 |
android:format24Hour | setFormat24Hour(CharSequence) | 设置 24 时制的格式 |
android:timeZone | setTimeZone(String) | 设置时区 |