搜尋此網誌

2012年11月25日 星期日

Regular expression


String pattern = "^(0{0,2}[0-9]|0?[1-9][0-9]|1[0-7][0-9])$";

^ :  代表一行的開始.
$: 代表一行的結束.
(  ) : 為capture group.
X | Y   : X 或 Y
X{n,m} : X 最少為n次,但不超過m次
[a-z] : a 到 z
X?  : x一次或完全沒有.

0{0,2}[0-9]

ex: 000 , 001 , 009


0?[1-9][0-9]

ex: 010  , 099


1[0-7][0-9])

ex : 100 ~ 179.

所以上列regular expression為找 000 到 179的 pattern寫法



2012年11月24日 星期六

原來Divide-and Conquer演算法,也可用在生活中

遇到很複雜又必須要處理的問題,可以使用分割-征服演算法來解決. 把大問題拆解成一個一個小問題來解決,當每個小問題都處理完畢. 自然地也把原來的大問題也同時解好了.

印出unicode字元

有些鍵盤找不到的符號,可以用unicode的方式來表達,如下例:

public class RegularExpression {
    
    public static void main(String [] args){
        char symbol = '\u00B0';
        System.out.println(symbol);
    }

}

UTF-8 的unicode table

2012年11月22日 星期四

CheckedTextView usage

CheckedTextView can be placed in the listview as a combination of checkbox and textview. This widget is very useful when you decide to let listview have a clickable item. CheckedTextView or you can check the list10 example under the view folder of android apidemos.

Focusable屬性

預設的設定上,使用者無法移動focus(焦點,也就是有外圍邊框的狀態)到此view, 若設為true,則使用者可以 拿到這個view的focus.

2012年11月14日 星期三

android在程式中設定背景顏色(background color)

要使用setBackgroundResource(R.color.white); 不可使用setBackgroundColor

2012年11月13日 星期二

Android 得到identifie resource id

Method 1: Using Java reflection features to get the resource ID.
public static int getResourceId(String name){
        int drawableId = -1 ;
        try {
            Class<drawable> res = R.drawable.class;
            Field field = res.getField(name);
            drawableId = field.getInt(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return drawableId;
    }
Method 2: Using Android Resources API.
public int getIdentifier (String name, String defType, String defPackage)

2012年11月8日 星期四

copy file from Android assets folder (由Android的assets資料夾拷貝檔案)

private void copyFile(String source, String destination) {
        InputStream srcStream;
        FileOutputStream desStream;
        try {
            srcStream = this.getResources().getAssets().open(source);
            desStream = new FileOutputStream(new File(destination));
            byte [] data= new byte [1024];
            while (srcStream.read(data) != -1) {
                desStream.write(data);
            }
            srcStream.close();
            desStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
add below expression in the AndroidManiFest.xml for getting access permission. 把這行加到AndroidManiFest.xml才有寫入權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

folder traversal (遞迴搜尋目錄找檔案)

package lang;

import java.io.File;

public class Hello {
    
    public static void main(String[] args){
        
        isFileExisted("." , "Test.java");
            
    
    }
    
    private static boolean isFileExisted(String targetPath , String fileName){
            
            File target = new File(targetPath);
            if(target.isDirectory()){
                File [] files = target.listFiles();
                if(files != null){
                    for(int i = 0 ; i<files.length ; i++){
                        isFileExisted(files[i].getAbsolutePath(), fileName); 
                    }    
                }
                
            }else {
                
                if(fileName.equalsIgnoreCase((target.getName()))){
                    return true;
                }
            }
            return false;
    }
    
}

2012年11月7日 星期三

open sqlite database file (打開sqlite database的檔案)

Step 1:
File dbfile = new File("/sdcard/dbname.sqlite" ); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

Step 2:

Write this permission expression in the AndroidManiFest.xml if you want to write a file into

device.



<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>



constraint failed

代表primary key 已經存在於table了.

2012年11月6日 星期二

打開android DDMS

Eclipse:的使用者可以 Click Window > Open Perspective > Other... > DDMS.

這樣就可以在開發程式的同時, 監控device.

OPEN DDMS

2012年11月1日 星期四

設定TextView的 text color(字體顏色)

myFolderText.setTextColor(getResources().getColor(R.color.blue));

單純用R.color.blue是無效的