搜尋此網誌

2012年6月28日 星期四

ArrayList 用法

import java.util.ArrayList;
import java.util.List;

public class CollectionsDemo {

    public static void main(String[] args) {
        List list = CollectionsDemo.getStudentList();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(((Student) list.get(i)).getID());
        }
    }

    private static List getStudentList() {
        List list = new ArrayList();
        list.add(new Student(1));
        list.add(new Student(2));
        return list;
    }

}

class Student {
    int id;

    Student(int id) {
        this.id = id;
    }

    public int getID() {
        return this.id;
    }
}

Javascript version, You can use array.pop() to remove elements.
 <html>  
 <head>  
 <script language = "javascript">  
 var array = [];  
 array.push(1);  
 array.push(1);  
 array.push(1);  
 </script>  
 </head>  
 <body>  
      <script language = "javascript">  
           alert(array.length);  
      </script>  
 </body>  
 </html>  
行程安排應用程式

2012年6月27日 星期三

Android手機設定全螢幕 . no title

<application
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        >

定義android ,hdpi landscape

在android , 1 dp = 1.5 pixels,

所以當拿到800*480 pixels的圖片,

換算成android dp為

533* 320 

因此要放在res/drawable-hdpi folder-->portrait case

res/drawable-land-hdpi-->landscape case

2012年6月25日 星期一

Android findViewById()

要create UI的Instance, 儘量用findViewById

若要創造非UI的Instance,才用new.

2012年6月15日 星期五

甚麼是介面Interface

Interface顧名思義是一個用來溝通的介面,

介面可以視為是一種Java object和外部世界的合約,和連結.

外部世界可以透過介面提供給Java class本身並沒有的功能.

而Java class也可以透過實做了介面所提供的方法, 自然的拿去給外部世界辨識和使用.




2012年6月14日 星期四

Open Source MSN software

如果不想要裝windows live,又想要使用msn的朋友,可以試看看下列的open source msn 軟體, 他是跨平台的,所以無論是linux or windows都可以使用. http://www.amsn-project.net/

2012年6月13日 星期三

Java Adapter design pattern

This pattern is very useful especially when programming language such as Java which does not

support multiple-inheritance. Following example can achieve multiple-inheritance by implementing an

adapter design pattern.


package pattern;

public interface Dog {
    public String getDogSound();
}


package pattern;

public interface Cat {
    
    public String getCatSound();

}


package pattern;

public class AnimalAdapter implements Cat,Dog {

    @Override
    public String getCatSound() {
        return "Cat sound";
    }

    @Override
    public String getDogSound() {
        return "Dog sound";
    }

}

AnimalAdapter have both cat's voice and dog's voice.

package pattern;

public class Simulator {
    
    static boolean cat = true;
    public static void main(String[]args){
        AnimalAdapter aa = new AnimalAdapter();
        if(cat){
            System.out.println(aa.getCatSound());
        }else if(!cat){
            System.out.println(aa.getDogSound());
        }
    
    }
    
}

Therefore , simulator can produce cat voice or dog voice in different situations.
Reference:Wiki Article

Android FrameLayout

Activity在切換的時候會被移到背景去 

若使用FrameLayout的話可以讓View重疊顯示,而不會移到背景去

而visibility這個屬性可以定義元件是否要被看見.

visible:看得見
invisible:看不見,但還是佔有空間.
gone:看不見,也不占空間.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:id="@+id/pop_btn"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="Click Me" 
        android:onClick="onClickBtn"
        />
    
    <Button android:id="@+id/pop_btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" 
        android:onClick="onClickBtn"
        />
</FrameLayout>

Android popup window demo

MainActivity.java
package com.example.android.popwindow;

import com.example.android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
    
    PopupWindow pw = null;

    boolean isClicked = false;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_main);
        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        pw = new PopupWindow(inflater.inflate(R.layout.pop_content,
                null, false), 100, 100, true);
        pw.setFocusable(false);
        
    }
    
    public void onClickBtn(View v){
        if(isClicked == true){
            pw.dismiss();
            isClicked = false;
        }else if(isClicked == false){
            pw.showAsDropDown(this.findViewById(R.id.pop_btn));
            isClicked = true;
        }
    }

}


We can use showAtLocation  to replace the showAsDropDown. Because this method is

more flexibile than the prior one.

res/layout/pop_content.xml


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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Content"
         />

</LinearLayout>
res/layout/pop_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:id="@+id/pop_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" 
        android:onClick="onClickBtn"
        />
</LinearLayout>

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="14" />

    <application android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme">
        
        <activity android:name="com.example.android.popwindow.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Reference: Android Developer Guide

2012年6月11日 星期一

如何獲得android md5 key

因為win7預設是使用sha1而不是md5簽章,

可以輸入下列指令來取得md5加密的簽章:

keytool -v -list -alias androiddebugkey -keystore "你的debug.keystore檔案的位置" -storepass android -keypass android



如何知道android keystore的位置

站長是在win7使用eclipse開發Android程式,

因此可以利用eclipse找到debug keystore的位置.

到preferences-> android-> build-> 就會看到預設的debug store目錄了. 


如何獲得MD5 key

2012年6月8日 星期五

Activity lifecycle, 生命週期.


onCreate() :
當activity第一次被創建出來的時候呼叫此方法.
我們可以在這一個階段作初始化的動作.
接下來進入到onStart().這個階段.

onRestart():
當activity由停止到開始之前呼叫.

onStart():
當activity開始被使用者看到會呼叫.

onResume():
當activity準備開始和使用者互動時呼叫.
在這個時間點,你的activity會在activity stack的頂端.

onPause():
當系統要開始復原前一個activity的時候呼叫.
Called when the system is about to start resuming a previous activity.

onStop():
當這個activity不再被使用者看到的時候呼叫.因為另一個activity已經被恢復了.

onDestroy():
在你的activity被摧毀之前,最後一個呼叫.





Android Button click handler implementation, 按鈕onclick監聽器實作

Here is implementation 1:

You can use this implementation style when your click event is definitely performed every time.

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/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" 
            />
</LinearLayout>
MainActivity.java
package com.example.android.demo;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.android.demo.R;

public class MainActivity extends Activity {

    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.button);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 Toast.makeText(MainActivity.class,"Hello, I am a Button" , Toast.LENGTH_SHORT);
             }
         });
    }
   
}

Here is implementation 2:

You can use this implementation style when your click event is not always executed.

This style is a little bit like the later-binding.

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/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" 
            android:onClick="onClickButton"
            />
</LinearLayout>
MainActivity.java
package com.example.android.demo;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.android.demo.R;

public class MainActivity extends Activity {

    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onClickButton(View v) {
        Toast.makeText(MainActivity.class,"Hello, I am a Button" , Toast.LENGTH_SHORT);
    }
   
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.demo"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="14" />

    <application android:label="@string/app_name">
        <activity android:name=".MainActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


補充:

public static interface

View.OnClickListener


這是一個靜態介面用來定義當view被按下去的時候,要做的動作.



public abstract void onClick (View v)


則是要實作的方法,參數View 為被按下的View本身.
 Reference :Android developer guide

2012年6月4日 星期一

android:gravity vs android:layout_gravity

android:gravity ~~~> 指元件自己放的位置. Specifies how to place the content of an object, both on the x- and y-axis, within the object itself. android:layout_gravity ~~~>指元件放在相對於父容器或元件的位置.
Reference

2012年6月1日 星期五

Android Handler demo

package my.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class HandlerDemoActivity extends Activity{
    
    int count = 0;
    public void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        final HandlerDemoActivity.MyHandler  handler = new MyHandler();
        Thread t = new Thread(){
            public void run(){
                while(true){
                    try {
                        Thread.sleep(2000);
                        Message msg = new Message();
                        Bundle msgBundle = new Bundle();
                        msgBundle.putInt("count", count);
                        msg.setData(msgBundle);
                        handler.sendMessage(msg);
                        count++;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                }
                                
            }
        };
        t.start();
    }
    
    class MyHandler extends Handler{
        public void handleMessage(Message msg){
            int i = (Integer) msg.getData().get("count");
            Log.d("handler", String.valueOf(i));
        }
        
    }
    

}

Surface View example

package my.surface;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceActivity extends Activity {
    SurfaceActivity.MyCallBack myCallBacks = new MyCallBack();
    SurfaceView sView = null; //Create an abstract view which can display something later.
    SurfaceHolder sHolder = null;//with holder to do actual painting
    
    //This class is created for actual behaviors when meets following status :surface create , destroy and change
    private class MyCallBack implements SurfaceHolder.Callback{

        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            
        }

        public void surfaceCreated(SurfaceHolder holder) {
            Canvas canvas = holder.lockCanvas();
            canvas.drawColor(Color.GREEN);
            holder.unlockCanvasAndPost(canvas);
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            
        }
        
    }
    
    public void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        sView = new SurfaceView(this);//This activity is treated as a context for the surface view.
        sHolder = sView.getHolder();//get a holder from this view to do control.
        sHolder.addCallback(myCallBacks);
        setContentView(sView);
    }

}