網頁
BloggerAds 廣告
標籤
- Java (96)
- Android (27)
- 演算法 (21)
- c++ (19)
- JavaScript (7)
- OpenMp (6)
- Design Pattern (4)
- 日文歌曲 (4)
- 資料結構 (4)
- Foundation Knowledge Of Programming (3)
- QUT (2)
- CodingHomeWork (1)
- Database (1)
- 英文歌詞 (1)
搜尋此網誌
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
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
補充:
public static interface
這是一個靜態介面用來定義當view被按下去的時候,要做的動作.
則是要實作的方法,參數View 為被按下的View本身.
Reference :Android developer guide
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
訂閱:
文章 (Atom)
我的網誌清單
標籤
日文歌曲
(4)
股市
(7)
股票
(9)
英文歌詞
(1)
時事
(1)
硬體(hardware)
(1)
資料結構
(4)
演算法
(21)
數學(Math)
(4)
ACM
(3)
ajax
(7)
algorithms
(1)
Android
(27)
Blog Notes(部落格記事)
(6)
C
(9)
c++
(19)
CodingHomeWork
(1)
Database
(1)
Design Pattern
(4)
Foundation Knowledge Of Programming
(3)
GWT
(1)
How
(2)
J2EE
(1)
Java
(96)
Java語言
(4)
JavaScript
(7)
Leetcode
(4)
LOL
(1)
OpenMp
(6)
QUT
(2)
Uva
(2)
Yahoo知識問答
(11)