android view中如何加linstener,

在開發中爲控件添加Listener是非常常見的工作,最簡單的添加Listener方式可以這樣:

Java代碼
1.findViewById(R.id.myButton).setOnClickListener(new View.OnClickListener() {
2. public void onClick(View v) {
3. // Do stuff
4. }
5.});
findViewById(R.id.myButton).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Do stuff } }); 採用上述方法添加Listener有個缺點就是如果控件太多的話,Listener數量也會增多,因此,可以採用如下的小竅門減少Listener的數量:

Java代碼
1.View.OnClickListener handler = View.OnClickListener() {
2. public void onClick(View v) {
3. switch (v.getId()) {
4. case R.id.Button01: // doStuff
5. break;
6. case R.id.Button02: // doStuff
7. break;
8. }
9. }
10.}
11.
12.findViewById(R.id.myButton).setOnClickListener(handler);
13.findViewById(R.id.myOtherButton).setOnClickListener(handler);
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的工作變得相當的簡單(感覺更像在做網頁編程!),具體步驟如下:

1.首先在layout裏面定義Button並指定響應的Listener

Xml代碼
1.<?xml version="1.0" encoding="utf-8"?>
2.<LinearLayout xmlns: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>
<?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的形參)。

Java代碼
1.package com.ray.test;
2.
3.import android.app.Activity;
4.import android.os.Bundle;
5.import android.view.View;
6.
7.public class TestOnClickListener extends Activity {
8.
9. @Override
10. public void onCreate(Bundle savedInstanceState) {
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.main);
13.
14.
15. }
16. public void myClickHandler01(View target){
17. setTitle("myClickHandler01");
18. }
19. public void myClickHandler02(View target){
20. setTitle("myClickHandler02");
21. }
22.}
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"); } }當然,你也可以採用這種寫法:

將兩個按鈕設置到同一個Listener

android:onClick="myClickHandler"

android:onClick="myClickHandler"

Java代碼
1.package com.ray.test;
2.
3.import android.app.Activity;
4.import android.os.Bundle;
5.import android.view.View;
6.
7.public class TestOnClickListener extends Activity {
8.
9. @Override
10. public void onCreate(Bundle savedInstanceState) {
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.main);
13. }
14. public void myClickHandler(View target){
15. switch (target.getId()) {
16. case R.id.Button01:
17. setTitle("myClickHandler01");
18. break;
19. case R.id.Button02:
20. setTitle("myClickHandler02");
21. break;
22. }
23. }
24.}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章