Android 获取 Raw 文件夹中资源地址

Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.adv);

使用 ProcessBuilder 处理通配符

喝着可乐听着歌;敲着代码哼着曲。没有一点点防备,就这样掉进了坑里。下面的代码是用通过ProcessBuilder调用命令来删除以system_开头的文件。 因为之前删除具体文件的时候这个方法非常好用,于是就想当然了。

ArrayList<String> cmd = new ArrayList<String>();

        cmd.add("rm");

        cmd.add("-vrf");

        cmd.add("/tmp/system_*");

        ProcessBuilder pb = new ProcessBuilder(cmd);

        pb.redirectErrorStream(true);

        try {

            Process process = pb.start();

            InputStream is = process.getInputStream();

            BufferedReader read = new BufferedReader(new InputStreamReader(is));

            String line;

            while ((line = read.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

正确的调用是这样的,通配符需要传给bash才能正确处理。

ArrayList<String> cmd = new ArrayList<String>();

        cmd.add("sh");

        cmd.add("-c");

        cmd.add("rm -vrf /tmp/system_*");

        ProcessBuilder pb = new ProcessBuilder(cmd);

        pb.redirectErrorStream(true);

        try {

            Process process = pb.start();

            InputStream is = process.getInputStream();

            BufferedReader read = new BufferedReader(new InputStreamReader(is));

            String line;

            while ((line = read.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

重写 ViewGroup 的 addView 方法

Viewgroup.addView()有5个重载方法:

void	addView(View child, int index, ViewGroup.LayoutParams params)

void	addView(View child, ViewGroup.LayoutParams params)

void	addView(View child, int index)

void	addView(View child)

void	addView(View child, int width, int height)

如果需要想重写addView()实际只需要重写**addView(View child, int index, ViewGroup.LayoutParams params)**即可。

看一下addView的源码:

    public void addView(View child) {
        addView(child, -1);
    }
    
    public void addView(View child, int index) {
        LayoutParams params = child.getLayoutParams();
        if (params == null) {
            params = generateDefaultLayoutParams();
            if (params == null) {
                throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
            }
        }
        addView(child, index, params);
    }


    public void addView(View child, int width, int height) {
        final LayoutParams params = generateDefaultLayoutParams();
        params.width = width;
        params.height = height;
        addView(child, -1, params);
    }
    
    public void addView(View child, LayoutParams params) {
        addView(child, -1, params);
    }

    /**
     * Adds a child view with the specified layout parameters.
     *
     * @param child the child view to add
     * @param index the position at which to add the child
     * @param params the layout parameters to set on the child
     */
    public void addView(View child, int index, LayoutParams params) {
        // addViewInner() will call child.requestLayout() when setting the new LayoutParams
        // therefore, we call requestLayout() on ourselves before, so that the child's request
        // will be blocked at our level
        requestLayout();
        invalidate();
        addViewInner(child, index, params, false);
    }

Swift Trailing Closure

func someFunctionThatTakesAClosure(closure: () -> () ) {

}

Not use trailing closure

someFunctionThatTakesAClosure({

})

Use trailing closure

someFunctionThatTakesAClosure() {

}

Example

let digitNames = [
    0: "Zero", 1: "One", 2: "two", 3: "Three", 4: "Foure",
    5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"
]

let numbers = [16, 58, 510]
let strings = numbers.map{
    (var number)->String in
    var output = ""
    while number > 0 {
        output = digitNames[number % 10]! + output
        number /= 10
    }
    return output
}
println(strings)

ref: The Swift Programming Language

Swift Switch Where Filter

好吧,swift这回要逆天了

let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let(x, y) where x == y:
    println("(\(x), \(y)) is on the line x == y")
case let(x, y) where x == -y:
    println("(\(x), \(y)) is on the line x == -y")
case let(x, y):
    println("(\(x), \(y)) is just some arbitary point")
}

ref: The Swift Programming Language