2013年8月4日 星期日

[Android] 如何監測長按結束 (Detect End of Long Press)

這個範例,我們來看看如何監看按壓按鈕的三個狀態
(This example will show you how to monitor click, long press and release button status)
1)點擊(click)
2)長按(long press)
3)放開(release)

File : MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class MainActivity extends Activity {

 boolean isBtnLongPressed = false;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Button btn = (Button) findViewById(R.id.button1);

  btn.setOnLongClickListener(new HoldListener());
  btn.setOnTouchListener(new touchListener());
  btn.setOnClickListener(new clickListener());
 }

 //監聽點擊(detect click)
 private class clickListener implements OnClickListener {

  @Override
  public void onClick(View view) {
   Log.e("log", "click button");
  }
 }

 //監聽長按(detect long press)
 private class HoldListener implements OnLongClickListener {

  @Override
  public boolean onLongClick(View pView) {
   // Do something when your hold starts here.
   isBtnLongPressed = true;
   Log.e("log", "long press button");
   return true;
  }

 }

 //監聽放開按鈕(detect release button)
 private class touchListener implements OnTouchListener {

  @Override
  public boolean onTouch(View pView, MotionEvent pEvent) {
   
   if (pEvent.getAction() == MotionEvent.ACTION_UP) {
    
    if (isBtnLongPressed) {
     // Do something when the button is released.
     isBtnLongPressed = false;
     Log.e("log", "release");
    }
   }
   return false;
  }
 }

}


2013年8月3日 星期六

[Android] How to add a Dropdown item on the action bar

Action Bar是android 3.0提供的一個功能,像下圖這樣
(The Action Bar  APIs were first added in Android 3.0 as following picture)




這個範例將教大家,如何在Action bar上加入下拉選單
(This example will teach you how to add a dropdown item on action bar)


File : MainActivity.java
import java.util.ArrayList;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ActionBar.OnNavigationListener;
import android.util.Log;
import android.widget.ArrayAdapter;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  final ActionBar actionBar = getActionBar();
  actionBar.setDisplayShowTitleEnabled(false);
  actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

  ArrayList itemList = new ArrayList();
  itemList.add("WMP");
  itemList.add("iTunes");
  itemList.add("Winamp");

  ArrayAdapter adapt = new ArrayAdapter(this,
    android.R.layout.simple_list_item_1, android.R.id.text1,
    itemList);
  actionBar.setListNavigationCallbacks(adapt, new DropDownListenser());
 }

 class DropDownListenser implements OnNavigationListener {

  public boolean onNavigationItemSelected(int itemPosition, long itemId) {
   if (itemPosition == 0) // windows media player
   {
    Log.e("log", "you choose WMP");
   }

   if (itemPosition == 1) // iTunes
   {
    Log.e("log", "you choose iTunes");
   }
   if (itemPosition == 2) // Winamp
   {
    Log.e("log", "you choose Winamp");
   }

   return true;

  }
 }

}

2013年7月31日 星期三

[Android] RelativeLayout範例介紹

在Android裡,RelativeLayout是一個最彈性的的Layout,
主要是利用上、下、左、右(above, below, left and right)來排列component的位置,
以我們這個例子來說,button2在button1的下面,button3在button2的右下方,button4在button3的右下方

File : res/layout/main.xml
<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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_toRightOf="@+id/button2"
        android:text="Button3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:text="Button4" />

</RelativeLayout>
結果:

[Android] TableLayout 範例介紹

在這個例子,我們來看如何用TableLayout來排列textView和button,
及如何使用“android:layout_span”將button寬度延伸成2欄位(column),
及使用“android:layout_column”來將按鈕放到特定的欄位。

File : res/layout/main.xml 
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <!-- 2個欄位 -->
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <TextView
            android:id="@+id/textView1"
            android:text="第 1 欄"
            android:textAppearance="?android:attr/textAppearanceLarge" />
 
        <Button
            android:id="@+id/button1"
            android:text="第 2 欄" />
    </TableRow>
 
    <!-- 將按鈕延伸至2個欄位寬  -->
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
         <Button
            android:id="@+id/button2"
            android:layout_span="2"
            android:text="第 1 和 2 欄" />
         
       
    </TableRow>
 
 
    <!-- 3個欄位 -->
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <TextView
            android:id="@+id/textView2"
            android:text="第 1 欄"
            android:textAppearance="?android:attr/textAppearanceLarge" />
 
        <Button
            android:id="@+id/button2"
            android:text="第 2 欄" />
 
        <Button
            android:id="@+id/button3"
            android:text="第 3 欄" />
    </TableRow>
 
    <!-- 利用layout_columnundefinedbase is 0),將按鈕放在第2欄 -->
    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <Button
            android:id="@+id/button4"
            android:layout_column="1"
            android:text="第 2 欄" />
    </TableRow>
 
    <!-- 利用layout_columnundefinedbase is 0),將按鈕放在第3欄 -->
    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <Button
            android:id="@+id/button5"
            android:layout_column="2"
            android:text="第  3 欄" />
    </TableRow>
 
</TableLayout>

結果:



















延伸閱讀: [Android] RelativeLayout範例介紹

2013年7月21日 星期日

[Android] LinearLayout 範例介紹

LinearLayout是一個很常用的Layout,
主用是用orientation屬性來把component(如button)做垂直及水平的排列,
另外,帶有權重(weight)的component將會填滿剩餘的空間

我們來舉個例子,來看看如何把component做垂直及水平的排列,及weight如何使用

1. LinearLayout – 水平(Horizontal)
打開res/layout/main.xml,加入 android:orientation="horizontal",然後增加3個按鈕,
在這個例子中,我們將權重設在button3,所以它將會填滿剩餘的空間

res/layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3" 
        android:layout_weight="1"/>
 
</LinearLayout>
結果:



2. LinearLayout – 垂直(Vertical)
我們把orientation改成“Vertical

res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3" 
        android:layout_weight="1"/>
 
</LinearLayout>
結果:

[Android] 解決問題Your project contains errors,please fix them before running your application.

今天開啟eclipse要使用時,發現無法編譯,只出現Your project contains errors,please fix them before running your application.









 檢查所有程式,也找不到任何錯誤,只有project name上出現一個紅色叉叉,
 一般這個問題,可能是無法讀取R.class,我會先刪除gen\R.java檔,然後refresh
 然後Project>clean整個project,最後重新編譯一次。
 但這次這個方法沒用,哈























最後google發現是Debug認證已經過期,解決方法很簡單,
進入windows>Preferences>Android->Build,找到debug.keystore的路徑,刪除就可以了,
系統會在自己產生一個

2013年7月7日 星期日

[Android] adb logcat使用方法

● Logcat是一個即時觀看系統訊息(system messages)的工具,一般會利用ADB(android debug bridge)來啟動,如果是用eclipse開發,就在windows\ShowView裡,選LogCat,就可以打開了。

這裡我們利用ADB來作示範,在裝完android SDK後,裡面就會包含了ADB,
以Windows為例,adb路徑會在android-sdks\platform-tools\adb.exe,所以在開啟terminal後,要先切到這個路徑下。
而Linux,裝好SDK,開啟terminal就可以用了。

使用之前裝置端要先開啟USB偵錯模式,路徑如下,設定>開發人員選項>USB偵錯
然後在電腦端輸入
$ adb devices
會得到下面結果,表示已和裝置端連接成功



● 下列是Log class提供了幾個列印message到logcat的方法
v(String, String) (verbose)
d(String, String) (debug)
i(String, String) (information)
w(String, String) (warning)
e(String, String) (error)

一般在開發程式時,會在每個function的的第一行加上log,方便日後trace code
像下面這樣
Log.i("MyActivity", "test"); //log.i裡的i是指(information)
LogCat就會輸出log成這樣
I/MyActivity( 1555): test


● 如何輸出LOG呢
$ adb logcat
但這種方法log太多了,會印出全部的log,包含verbose/debug/information/warning/error
所以我們需要用到下列的方法過濾出我們要的部分


● 過濾log(Filtering Log Output)
每一個Android的log都包含了標籤(tag),和優先權(priority)
例如,I/MyActivity( 1557): test //I是優先權,又分成了下列幾種;MyActivity就是標籤
Android優先權:
V — Verbose (最低優先權)
D — Debug
I — Info
W — Warning
E — Error
F — Fatal
S — Silent (最高優先權,S表示不會用印出任何LOG)
為什麼分成七級呢?看到例子三就明白了

知道了android log之後,要如何過濾呢?我們用下面幾個例子來學習學習
例子一
//如果只想印出這種寫法Log.i("MyActivity", "test")的log,則要這樣寫

$ adb logcat MyActivity:I *:S

//MyActivity:I是表示,印出MyActivity標籤及information優先權的log

//*:S 是指其它所有的log都silent,也就是都不印出來

例子二
//如果我們一次想看這2種logs,則該怎麼寫呢?

//Log.i("MyActivity", "test");

//Log.d("MyActivity2", "test2");

$ adb logcat MyActivity:I MyActivity2:D *:S

例子三
那如果這樣寫$ adb logcat *:W 會發生什麼事呢?

這樣就會印出所有log,且優先權在Warning以上的log,也就是Warning/Error/Fatal



● 延伸
一般我們在做phone的project時,就會常用到
$ adb logcat -v time -b radio
//-v time是指出時間,-b radio是指印出radio和telephony相關訊息


●更多參考資料:
http://developer.android.com/tools/debugging/debugging-log.html#startingLogcat