搜尋此網誌

2012年6月8日 星期五

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

沒有留言:

張貼留言