2011年12月29日 星期四

Android - 取消Activity動畫

如果程式會連續開啟相同畫面的Activity,卻又不希望每次都有動畫出現,可以在onCreate()加上

getWindow().setWindowAnimations(0);


另外如果是用intent呼叫Activity時,也可以使用

Intent it = new Intent("com.gill.test.intentactivity");
it.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(it);

Eclipse hotkey (熱鍵、快捷鍵)

ctrl + z 回復上一次步驟
ctrl + y 重做下一次步驟

ctrl + / 註解 (取消註解)

ctrl + k 快速搜尋關鍵字 (如果先用ctrl + f 然後再找下一筆)
ctrl + f 搜尋關鍵字

ctrl + shift + o 自動匯入所欠缺的類別

ctrl + l 移至指定行數

ctrl + 滑鼠左鍵 跳至定義位置
alt + ← 跳到上一次游標所在位置
alt + → 跳到下一次游標所在位置
(這個功能常配合ctrl + 滑鼠左鍵,當查完定義後,要再回到原程式位置)

ctrl + shift + s 全部儲存

ctrl + shift + p 跳至匹配的括號
ctrl + q 跳至上一次編輯的位置

-----------------------------------------------------------------------------------------------------

ctrl + shift + L 顯示按鍵輔助

alt + / 程式碼輔助

alt + ↑ 上移 (快速的移動一行或一個段落)
alt + ↓ 下移 (快速的移動一行或一個段落)

ctrl + shift + k 快速搜尋關鍵字 (如果先用ctrl + f 然後再找上一筆)
ctrl + o 搜尋所有位於function名的關鍵字
ctrl + s 儲存

ctrl + D 單行刪除

ctrl + j 向下增量搜尋 (每打1個字母就開始向下搜尋)
ctrl + shift +j 向上增量搜尋 (每打1個字母就開始向上搜尋)

ctrl + shift + f 格式化程式碼

紅色字體為較少見,但是實用的hotkey

Android - 產生scroll bar讓使用者往下拉

我們可以把所有的元件放到ScrollView裡面,這樣就可以在畫面產生scroll bar讓使用者往下拉。但是,ScrollView裡面只可以作用於一個元件,最常見的做法就是,在ScrollView裡面放一個LinearLayout,再把所有的元件放在這個LinearLayout裡面就可以了。

xml範例如下:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

   <ScrollView   
             android:layout_width="fill_parent"   
             android:layout_height="wrap_content" > 

   
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is a TextView" />

    <CheckBox
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="This is a CheckBox" />

    </LinearLayout>

  </ScrollView>

</LinearLayout>

Android - Layout Tool

想要簡單而且很快的繪製程式的畫面,可以使用一個叫的工具。無論是改變元件大小或是位置,都相單快速。就像VB等開發工具。可以從這裡下載程式。畫好畫面只要按下「Generate」就可以產生layout的 xml檔案。












Android - View常見的attribute

layout_marginTop = "10px" 本元件和上元件距離10px
layout_marginBottom = "10px" 本元件和下元件距離10px
layout_marginLeft = "10px" 本元件和左元件距離10px
layout_marginRight = "10px" 本元件和右元件距離10px

singleLine="true"  文字內容最多只有一行,常用於TextView元件

layout_above = "@id/xxxxx" 本元件放在xxxxx元件的上方
layout_below = "@id/xxxxx" 本元件放在xxxxx元件的下方
layout_toLeftOf = "@id/xxxxx" 本元件放在xxxxx元件的左方
layout_toRightOf = "@id/xxxxx" 本元件放在xxxxx元件的右方

layout_alignTop = "@id/xxxxx" 本元件頂部和xxxxx元件頂部切齊
layout_alignBottom = "@id/xxxxx" 本元件底部和xxxxx元件底部切齊
layout_alignLeft = "@id/xxxxx" 本元件左側和xxxxx元件左側切齊
layout_alignRight = "@id/xxxxx" 本元件右側和xxxxx元件右側切齊

layout_alignParentTop = "true" 本元件頂部和外層父元件頂部切齊
layout_alignParentBottom = "true" 本元件底部和外層父元件底部切齊
layout_alignParentLeft = "true" 本元件左側和外層父元件左側切齊
layout_alignParentRight = "true" 本元件右側和外層父元右側切齊

layout_centerHorizontal = "true" 本元件位於水平方向的中央
layout_centerVertical = "true" 本元件位於垂直方向的中央
layout_centerInParent = "true" 本元件位於外層父元件內,水平和垂直方向的中央

background="@drawable/background_pic" 指定background_pic為背景圖片
background="#000000" 指定背景為黑色

visibility="gone" 該元件不可視,而且不佔用面積

Android - Drawable 轉成 Bitmap

在Drawable資料夾中有一檔案為download.png,如果要轉成 Bitmap格式,可用程式:

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.download);

Android - IntentService使用

請先看有關IntentService官網介紹,另外這裡這裡也 有詳細說明原理。這裡用程式實作,並發現,一般的Service執行完後並沒有停止,在 Settings->Applications->Running services中可以發現依然存在,但是如果程式改用IntentService,則會發現執行程式後,在 Settings->Applications->Running services 是看不到的。

程式有2個檔案如下:
Main.java = 一個Activity的主畫面,按下畫面的按鈕可以呼叫Service
OriginalService.java = 繼承IntentService

Main.java 如下:
public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btn = (Button) findViewById(R.id.btn1);    
        OnClickListener lis = new OnClickListener() {       
            public void onClick(View v) {
                Intent it = new Intent();
                it.setClass(Main.this, OriginalService.class);
                startService(it);
            }
        };
        btn.setOnClickListener(lis);
       
    }
}


//OriginalService.java 如下:
public class OriginalService extends IntentService {

    //這個constructor一定要寫,不然在runtime的時候,會出現
    // java.lang.InstantiationException 的錯誤
    public OriginalService(){
        super("OriginalService");
    }
   
    public OriginalService(String name) {
        super(name);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("gill", "onStartCommand, the hashcode = " + this.toString());
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //這裡寫上任何想要在背景耗時的作業,比如網路下載,讀取資料庫,播放音樂音效…
    }

}