0%

Android string.xml 插值的简单应用

Android string.xml 插值的简单应用

在string.xml中定义

 <string name = “myname”>他今年%1$d岁了</string>

在程序中

String string = context.getResource().getString(R.string.myname);

MyTextView.setText(String.format(string,8));

打印 他今年8岁了

%d 整数

%f 浮点数

%s字符串

也可以在String.xml中%2添加多个值,依次在代码中填入相应值即可

Android studio打开无响应

Android studio打开无响应

双击666后无任何反应

有两种可能

1.gradle版本和AS不对应

2.环境变量没配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="@color/bottom_controller_color" />

<!-- 边框的颜色和粗细 -->
<stroke
android:width="1dp"
android:color="@color/bottom_controller_color"
/>

<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:radius="20dp"
android:topLeftRadius="0dp"
android:topRightRadius="18dp"
/>

</shape>

Android 源码初步认识

Android 源码初步认识

1.源码结构

"BootLoader" u-boot.img MLO 连接硬件和操作系统的中间件,检测与配置硬件,启动操作系统

"uImage"  Linux内核,被u-boot识别引导

"ramdisk.img"  Android系统根文件镜像

"boot.img"  将zImage和ramdisk.img合并为一个包

"system.img"  Android系统核心  将被挂载到 /system分区

"userdata.img" 用户文件  将被挂载到 /data分区


android在线源码:http://www.grepcode.com/


Android切九妹图片

Android切九妹图片

1.打开sdk/tools/draw9patch

2.拖进去一张图片

3.四周都可以点,左键选择锚点

4.左上为图片拉伸区域,右下为内容固定区域

口诀,catch优先return, 无论怎样finally
2019年9月19日

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private void go() {
tag = "ldo-";
Log.d(tag, "go: start");
try {
Log.d(tag, "go: try 01");
arrayList.get(0);
Log.d(tag, "go: return 01");
return;
} catch (Exception e) {
Log.d(tag, "go: catch01");
e.printStackTrace();
try {
arrayList = new ArrayList<>();
arrayList.add(1);
} catch (Exception e1) {
e1.printStackTrace();
Log.d(tag, "go: catch02");
}

} finally {
Log.d(tag, "go: finally");
}
}

方法第一次执行
猜猜看打印什么
嘟嘟嘟嘟
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
..
.
.
.

公布

1
2
3
4
01-01 01:24:02.567 17772-17772/ D/ldo-: go: start
01-01 01:24:02.567 17772-17772/ D/ldo-: go: try 01
01-01 01:24:02.567 17772-17772/ D/ldo-: go: catch01
01-01 01:24:02.570 17772-17772/ D/ldo-: go: finally

第二次执行
嘟嘟嘟
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

1
2
3
4
01-01 01:27:29.302 17772-17772/ D/ldo-: go: start
01-01 01:27:29.302 17772-17772/ D/ldo-: go: try 01
01-01 01:27:29.302 17772-17772/ D/ldo-: go: return 01
01-01 01:27:29.302 17772-17772/ D/ldo-: go: finally

别忘了会执行finally哦~


最后考大家一个, 大家加油哦 ~. ~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 private void go() {
tag = "ldo-";
Log.d(tag, "go: start");
try {
Log.d(tag, "go: try 01");
arrayList.get(0);
Log.d(tag, "go: return 01");
return;
} catch (Exception e) {
Log.d(tag, "go: catch01");
e.printStackTrace();
try {
// arrayList = new ArrayList<>();
arrayList.add(1);
Log.d(tag, "go: try 02");
Log.d(tag, "go: return 02");
return;
} catch (Exception e1) {
e1.printStackTrace();
Log.d(tag, "go: catch02");
return;
}finally {
Log.d(tag, "go: finally 02");
}

} finally {
Log.d(tag, "go: finally 01");
}
}

2.所有对象都有的方法(equals,hashCode…)

Item8 遵循equals的通用规则
  • 反射性:if x != null ,x.equals(x) must return true;
  • 对称性:if x != null && y != null ,x.equals(y) is true; y.equals(x) must return true
  • 传递性:if x != null && y != null && z != null ,x.equals(y) is true; y.equals(z) is true ; x.equals(z) must return true
  • 一致性:不管调用多少次equals方法,返回的值一致
  • if x != null,x.equals(null) must return false

    除非有数学上的倾向要违背这些,否则必要遵循
    当父类和子类都有自己的equals方法时,某些场景会违背这些原则,非要使用equals的话,建议将子类替换成组合的方式
    当自己重写equals方法问自己三个问题:它是对称性的吗?是可传递的吗?是一致的吗?
    #####Item9 重写equals时始终重写hashCode
    重写equals而没有重写hashCode会导致用到hash值的类出现bug,如HashMap,HashSet..
    下面是使用Object在JavaSE6中的规则:

  • 在一次程序操作中的同一个对象,任何时候hashCode保持一致
  • 如果两个对象根据equals(Object)方法相等,那么对这两个对象中的每个对象调用hashCode方法都必须产生相同的整数结果
  • 如果两个对象根据equals(Object)方法不相等,那么对这两个对象中的每个对象没必要产生相同的整数结果,但是有利于提高HashTable的性能

    生成hashCode时,属性中可计算的则直接参与计算,不可计算的字段调用它的hashCode()参与计算

Item10 始终重写toString方法

不重写的话会得到Object@163b91一串无意义的打印
最好是打印出自己需要的关键字段信息

Item11 明智的重写clone方法

使用的较少,理解不深,后续补充

Item12 实现 Comparable接口

对于需要排序和比较的对象,实现此接口定义自己的排序规则


上一章:创建和销毁对象
下一章:类和接口

高效使用异常指南

Item 57: Use exceptions only for exceptional conditions

只有在异常条件下才使用异常

考虑如下的代码

1
2
3
4
5
6
//bad !! don't do this
try {
int i = 0;
while(true)
range[i++].climb();
} catch(ArrayIndexOutOfBoundsException e){}

使用异常条件来终止遍历操作是非常错误的做法

使用如下代码代替

1
2
for (Mountain m : range)
m.climb();

名副其实,异常只能用在异常条件下,而不能用于流程控制


Item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors

对可恢复条件使用检查异常,对程序错误使用运行时异常。

Java提供了三种异常:checked exceptions,runtime exceptions and errors.大多数程序员对何时使用何种异常有困惑,有如下几种原则作参考

决定使用检查异常或非检查异常的原则是:调用方可以合理的修复异常,现如今的IDE工具如eclipse或Android studio会自动提示此类异常,自动填充try catch

非检查异常有两种:runtime exceptions and errors

用运行时异常(runtime exceptions)识程序错误,绝大多数的运行时异常都表明违反了某种前提条件,如ArrayIndexOutOfBoundsException数组越界

有一个普遍的约定是error用于JVM,所以所有的非检查异常都应该继承自 RuntimeException

常见的非检查异常runtime exception

  • NullPointerException, 空指针异常
  • ArithmeticException, 算术异常
  • ClassCastException, 类型强制转换异常
  • IllegalArgumentException, 传递非法参数异常

Item 59: Avoid unnecessary use of checked exceptions

避免过度使用检查异常
如果一个方法有多个检查异常,调用者会包裹多个catch来处理异常,这里没有一个绝对的准则,可以使用if语句把条件先过滤一遍而避免抛异常


Item 60: Favor the use of standard exceptions

使用常用异常
当需要抛出一个异常时,尽量使用Java平台提供好的异常,因为大家都知道什么意思


Item 61: Throw exceptions appropriate to the abstraction

抛出更合适的抽象异常

当抛出和任务不相关的异常时容易让人困惑,当抛出一个低级的异常时,这种情况经常发生,它不仅仅令人困惑,也污染了上层的调用

为了避免这个问题,上层应该捕获较低级别的异常,并在它们的位置上抛出可以用更高级别的抽象来解释的异常,如:

1
2
3
4
5
6
7
// Exception Translation
try {
// Use lower-level abstraction to do our bidding
...
} catch(LowerLevelException e) {
throw new HigherLevelException(...);
}

更具体的例子在List的方法中

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* Returns the element at the specified position in this list.
* @throws IndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index >= size()}).
*/
public E get(int index) {
ListIterator<E> i = listIterator(index);
try {
return i.next();
} catch(NoSuchElementException e) {
throw new IndexOutOfBoundsException("Index: " + index);
}
}

这种特殊的异常处理方式被称为”异常链”,但是也不应该过度使用

我们也可以在在上层调用中抛出底层错误的原因

1
2
3
4
5
6
// Exception Chaining
try {
... // Use lower-level abstraction to do our bidding
} catch (LowerLevelException cause) {
throw new HigherLevelException(cause);
}

最好的方式还是尽量在底层避免异常,如果不能处理在考虑”异常链”的方式


Item 62: Document all exceptions thrown by each method

为每个方法抛出的异常添加文档注释

对于检查异常,要说明前置条件,并用@throws标记合适的异常,不要为了图简单直接 @throws Exception

如果一个异常被多个方法抛出,并且是相同的原因,则不要在方法上注释,而是要在类上添加异常注释


Item 63: Include failure-capture information in detail messages

打印出异常的详细信息以便于分析


Item 64: Strive for failure atomicity

保证错误的原子性(调用一千次,输出一样)

即使一个异常发生了,我们也希望对象能正常使用

有几种方式可以达到这一点,最简单的就是创建不可变的对象,如果一个对象是不可变的,原子性也随之而来

对于可变对象,常见的方式是检查参数的合法性(Item 38)

第三种方式是发生异常后,回滚异常状态为使用前的初始状态

最后一种方式使用拷贝来避免发生错误时改变原来对象的状态,如Collections.sort,排序方法会先转成array数组来排序,本来是为了提高性能,额外的如果发生了错误不会改变原集合的状态

虽然原子性能保证,但是实际使用中并不总是能达到满意的状态,如两个线程同时修改一个对象,没有同步的情况下,引发了currentModificationException.这时如果恢复对象的状态依然不能使程序正确运行

作为一种规则来讲,当发生异常时总能确保当前对象的状态,可惜目前很多API都没有遵守


Item 65: Don’t ignore exceptions

不要忽略异常

这条建议看似很明显,但是值得重复,当API的设计者声明了一种异常,他们是为了告诉你什么,**不要忽略它!**很容易包裹一个空的try catch语句就不管这个异常了.如:

1
2
3
4
try {
...
} catch (SomeException e) {
}

空处理并不是抛异常的初衷,目的是为了强制解决这个异常条件.忽略异常就像忽略火警广播,有人关了广播导致其他人并不知道发生了火灾.如果确实需要一个空的处理,需要详细说明为何空处理是合适的


上一章:通用原则

Android截取string中的空字符

Android截取string中的空字符

最近遇到一个需求,在textview显示执行控制台“df”命令返回的字符串 排版那叫一个难看,所以想截取出来自己重新计算位置。

大家只要记住就可以了,用到了正则表达式

String.split(“\\s+”)   截取多个空格 结果保存在String[]里

Application官方直译深入理解(持续更新中)

Application官方直译深入理解(持续更新中)

Application

持有全局应用的基类,通过继承Application并在清单文件声明name属性来使用自己的application。

Note:通常情况下我们并不需要自己的application,为了获得一个全局的context,可以通过Context.getApplicationContext()来得到一个context