Android View添加 Listener 實(shí)例代碼
findViewById(R.id.myButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do stuff
}
});
采用上述方法添加Listener有個缺點(diǎn)就是如果控件太多的話,Listener數(shù)量也會增多,因此,可以采用如下的小竅門減少Listener的數(shù)量:
View.OnClickListener handler = View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button01: // doStuff
break;
case R.id.Button02: // doStuff
break;
}
}
}
findViewById(R.id.myButton).setOnClickListener(handler);
findViewById(R.id.myOtherButton).setOnClickListener(handler);
在Android1.6里面,添加Listener的工作變得相當(dāng)?shù)暮唵危ǜ杏X更像在做網(wǎng)頁編程!),具體步驟如下:
1.首先在layout里面定義Button并指定響應(yīng)的Listener
<xml version="1.0" encoding="utf-8"?>
<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="@string/hello"
/>
<Button
android:text="Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler01"
/>
<Button
android:text="Button02"
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler02"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
其中以下這兩行就是新增的特性:
android:onClick="myClickHandler01"
android:onClick="myClickHandler02"
2.在活動里面定義public的方法myClickHandler01、和myClickHandler02(注意這兩個方法必須有一個View的形參)。
package com.ray.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class TestOnClickListener extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void myClickHandler01(View target){
setTitle("myClickHandler01");
}
public void myClickHandler02(View target){
setTitle("myClickHandler02");
}
}
當(dāng)然,你也可以采用這種寫法:
將兩個按鈕設(shè)置到同一個Listener
android:onClick="myClickHandler"
android:onClick="myClickHandler"
package com.ray.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class TestOnClickListener extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void myClickHandler(View target){
switch (target.getId()) {
case R.id.Button01:
setTitle("myClickHandler01");
break;
case R.id.Button02:
setTitle("myClickHandler02");
break;
}
}
}
相關(guān)文章
Android中手機(jī)錄屏并轉(zhuǎn)換GIF的兩種方式
本文主要介紹了android中手機(jī)錄屏并轉(zhuǎn)換GIF的兩種方式,具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01
Android List(集合)中的對象以某一個字段排序案例
這篇文章主要介紹了Android List(集合)中的對象以某一個字段排序案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
android自定義toast(widget開發(fā))示例
這篇文章主要介紹了android自定義toast(widget開發(fā))示例,需要的朋友可以參考下2014-03-03
Android 實(shí)現(xiàn)自定義圓形進(jìn)度條的實(shí)例代碼
進(jìn)度條在Android中教程使用到,本文章向大家介紹一下Android自定義圓形進(jìn)度條實(shí)現(xiàn)代碼,需要的朋友可以參考一下。2016-11-11
GuideView的封裝實(shí)現(xiàn)app功能引導(dǎo)頁
這篇文章主要為大家詳細(xì)介紹了GuideView的封裝實(shí)現(xiàn)app功能引導(dǎo)頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03

