android view中如何加linstener,

在开发中为控件添加Listener是非常常见的工作,最简单的添加Listener方式可以这样:

Java代码

1.findViewById(R.id.myButton).setOnClickListener(newView.OnClickListener(){

2.publicvoidonClick(Viewv){

3.//Dostuff

4.}

5.});

findViewById(R.id.myButton).setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//Dostuff}});采用上述方法添加Listener有个缺点就是如果控件太多的话,Listener数量也会增多,因此,可以采用如下的小窍门减少Listener的数量:

Java代码

1.View.OnClickListenerhandler=View.OnClickListener(){

2.publicvoidonClick(Viewv){

3.switch(v.getId()){

4.caseR.id.Button01://doStuff

5.break;

6.caseR.id.Button02://doStuff

7.break;

8.}

9.}

10.}

11.

12.findViewById(R.id.myButton).setOnClickListener(handler);

13.findViewById(R.id.myOtherButton).setOnClickListener(handler);

View.OnClickListenerhandler=View.OnClickListener(){publicvoidonClick(Viewv){switch(v.getId()){caseR.id.Button01://doStuffbreak;caseR.id.Button02://doStuffbreak;}}}findViewById(R.id.myButton).setOnClickListener(handler);findViewById(R.id.myOtherButton).setOnClickListener(handler);在Android1.6里面,添加Listener的工作变得相当的简单(感觉更像在做网页编程!),具体步骤如下:

1.首先在layout里面定义Button并指定响应的Listener

Xml代码

1.<?xmlversion="1.0"encoding="utf-8"?>

2.<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

3.android:orientation="vertical"

4.android:layout_width="fill_parent"

5.android:layout_height="fill_parent"

6.>

7.<TextView

8.android:layout_width="fill_parent"

9.android:layout_height="wrap_content"

10.android:text="@string/hello"

11./>

12.<Button

13.android:text="Button01"

14.android:id="@+id/Button01"

15.android:layout_width="wrap_content"

16.android:layout_height="wrap_content"

17.android:onClick="myClickHandler01"

18./>

19.<Button

20.android:text="Button02"

21.android:id="@+id/Button02"

22.android:layout_width="wrap_content"

23.android:layout_height="wrap_content"

24.android:onClick="myClickHandler02"

25./>

26.<TextView

27.android:layout_width="fill_parent"

28.android:layout_height="wrap_content"

29.android:text="@string/hello"

30./>

31.</LinearLayout>

<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><Buttonandroid:text="Button01"android:id="@+id/Button01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="myClickHandler01"/><Buttonandroid:text="Button02"android:id="@+id/Button02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="myClickHandler02"/><TextViewandroid: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的形参)。

Java代码

1.packagecom.ray.test;

2.

3.importandroid.app.Activity;

4.importandroid.os.Bundle;

5.importandroid.view.View;

6.

7.publicclassTestOnClickListenerextendsActivity{

8.

9.@Override

10.publicvoidonCreate(BundlesavedInstanceState){

11.super.onCreate(savedInstanceState);

12.setContentView(R.layout.main);

13.

14.

15.}

16.publicvoidmyClickHandler01(Viewtarget){

17.setTitle("myClickHandler01");

18.}

19.publicvoidmyClickHandler02(Viewtarget){

20.setTitle("myClickHandler02");

21.}

22.}

packagecom.ray.test;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;publicclassTestOnClickListenerextendsActivity{@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);}publicvoidmyClickHandler01(Viewtarget){setTitle("myClickHandler01");}publicvoidmyClickHandler02(Viewtarget){setTitle("myClickHandler02");}}当然,你也可以采用这种写法:

将两个按钮设置到同一个Listener

android:onClick="myClickHandler"

android:onClick="myClickHandler"

Java代码

1.packagecom.ray.test;

2.

3.importandroid.app.Activity;

4.importandroid.os.Bundle;

5.importandroid.view.View;

6.

7.publicclassTestOnClickListenerextendsActivity{

8.

9.@Override

10.publicvoidonCreate(BundlesavedInstanceState){

11.super.onCreate(savedInstanceState);

12.setContentView(R.layout.main);

13.}

14.publicvoidmyClickHandler(Viewtarget){

15.switch(target.getId()){

16.caseR.id.Button01:

17.setTitle("myClickHandler01");

18.break;

19.caseR.id.Button02:

20.setTitle("myClickHandler02");

21.break;

22.}

23.}

24.}

相关推荐