码迷,mamicode.com
首页 > 其他好文 > 详细

EventDemo

时间:2015-08-06 13:05:57      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

Event Handling 示例:

分为EventListener、

EventListenerRegistration和EventHandler。

注册Event的三种方法:

1)  在Activity类中使用匿名内部类(anonymous inner class):可以用于对单个控件的处理,也可以用于对多个控件的处理,当处理多个控件需要复制代码,能够传递参数。 new OnClickListener(){

                                 public void onClick(View v){}

                         }

2)  在Activity类中实现Listener接口:可以用于对单个控件的处理,也可以用于对多个控件的处理,当处理多个控件时,需要判断产生event的具体是哪个控件,从而做出不同的响应,不能传递参数。implements OnClickListener

3)  使用layout文件activity_main.xml直接实现:处理函数的返回类型必须是void,参数是View,函数名任取,不能传递参数,只能处理click事件。android:onClick

 

1)  在Activity类中使用匿名内部类

技术分享
 1 package com.example.shad_fnst.eventdemo;
 2 
 3 import android.support.v7.app.ActionBarActivity;
 4 import android.os.Bundle;
 5 import android.view.Menu;
 6 import android.view.MenuItem;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 
12 public class MainActivity extends ActionBarActivity {
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18 
19         Button buttonSmall = (Button) findViewById(R.id.btnSmall);
20         Button buttonLarge = (Button) findViewById(R.id.btnLarge);
21 
22         buttonSmall.setOnClickListener(new View.OnClickListener() {
23             @Override
24             public void onClick(View v) {
25                 TextView textView = (TextView) findViewById(R.id.txtview);
26                 textView.setTextSize(24);
27                 textView.setAllCaps(true);
28             }
29         });
30 
31         buttonLarge.setOnClickListener(new View.OnClickListener() {
32             @Override
33             public void onClick(View v) {
34                 TextView textView = (TextView) findViewById(R.id.txtview);
35                 textView.setTextSize(40);
36                 textView.setTextColor(0xff32cebb);
37             }
38         });
39     }
40 
41     @Override
42     public boolean onCreateOptionsMenu(Menu menu) {
43         // Inflate the menu; this adds items to the action bar if it is present.
44         getMenuInflater().inflate(R.menu.menu_main, menu);
45         return true;
46     }
47 
48     @Override
49     public boolean onOptionsItemSelected(MenuItem item) {
50         // Handle action bar item clicks here. The action bar will
51         // automatically handle clicks on the Home/Up button, so long
52         // as you specify a parent activity in AndroidManifest.xml.
53         int id = item.getItemId();
54 
55         //noinspection SimplifiableIfStatement
56         if (id == R.id.action_settings) {
57             return true;
58         }
59 
60         return super.onOptionsItemSelected(item);
61     }
62 }
MainActivity.java

2)  在Activity类中实现Listener接口

技术分享
 1 package com.example.shad_fnst.eventdemo;
 2 
 3 import android.support.v7.app.ActionBarActivity;
 4 import android.os.Bundle;
 5 import android.view.Menu;
 6 import android.view.MenuItem;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 
12 public class MainActivity extends ActionBarActivity implements View.OnClickListener{
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18 
19         Button buttonSmall = (Button) findViewById(R.id.btnSmall);
20         Button buttonLarge = (Button) findViewById(R.id.btnLarge);
21 
22         buttonSmall.setOnClickListener(this);
23         buttonLarge.setOnClickListener(this);
24     }
25 
26     @Override
27     public void onClick(View v) {
28         if(v ==  findViewById(R.id.btnSmall)){
29             TextView textView = (TextView) findViewById(R.id.txtview);
30             textView.setTextSize(24);
31             textView.setAllCaps(true);
32             return;
33         }
34         if(v.getId() == R.id.btnLarge){
35             TextView textView = (TextView) findViewById(R.id.txtview);
36             textView.setTextSize(40);
37             textView.setTextColor(0xff32cebb);
38             return;
39         }
40     }
41 
42     @Override
43     public boolean onCreateOptionsMenu(Menu menu) {
44         // Inflate the menu; this adds items to the action bar if it is present.
45         getMenuInflater().inflate(R.menu.menu_main, menu);
46         return true;
47     }
48 
49     @Override
50     public boolean onOptionsItemSelected(MenuItem item) {
51         // Handle action bar item clicks here. The action bar will
52         // automatically handle clicks on the Home/Up button, so long
53         // as you specify a parent activity in AndroidManifest.xml.
54         int id = item.getItemId();
55 
56         //noinspection SimplifiableIfStatement
57         if (id == R.id.action_settings) {
58             return true;
59         }
60 
61         return super.onOptionsItemSelected(item);
62     }
63 }
MainActivity.java

方法1和方法2的xml文件

技术分享
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 3     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
 4     android:paddingRight="@dimen/activity_horizontal_margin"
 5     android:paddingTop="@dimen/activity_vertical_margin"
 6     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
 7     android:orientation="vertical">
 8 
 9     <Button
10         android:id="@+id/btnSmall"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:text="@string/button_small"/>
14     <Button
15         android:id="@+id/btnLarge"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:text="@string/button_large"/>
19 
20     <TextView
21         android:id="@+id/txtview"
22         android:text="@string/hello_world"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:capitalize="characters"/>
26 
27 </LinearLayout>
main_activity.xml

 

3)  使用layout文件activity_main.xml直接实现

技术分享
 1 package com.example.shad_fnst.eventdemo;
 2 
 3 import android.support.v7.app.ActionBarActivity;
 4 import android.os.Bundle;
 5 import android.view.Menu;
 6 import android.view.MenuItem;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 
12 public class MainActivity extends ActionBarActivity{
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18     }
19 
20     public  void doSmall(View view){
21         TextView textView = (TextView) findViewById(R.id.txtview);
22         textView.setTextSize(24);
23         textView.setAllCaps(true);
24     }
25 
26     public void doLarge(View view){
27         TextView textView = (TextView) findViewById(R.id.txtview);
28         textView.setTextSize(40);
29         textView.setTextColor(0xff32cebb);
30     }
31 
32     @Override
33     public boolean onCreateOptionsMenu(Menu menu) {
34         // Inflate the menu; this adds items to the action bar if it is present.
35         getMenuInflater().inflate(R.menu.menu_main, menu);
36         return true;
37     }
38 
39     @Override
40     public boolean onOptionsItemSelected(MenuItem item) {
41         // Handle action bar item clicks here. The action bar will
42         // automatically handle clicks on the Home/Up button, so long
43         // as you specify a parent activity in AndroidManifest.xml.
44         int id = item.getItemId();
45 
46         //noinspection SimplifiableIfStatement
47         if (id == R.id.action_settings) {
48             return true;
49         }
50 
51         return super.onOptionsItemSelected(item);
52     }
53 }
MainActivity.java
技术分享
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 3     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
 4     android:paddingRight="@dimen/activity_horizontal_margin"
 5     android:paddingTop="@dimen/activity_vertical_margin"
 6     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
 7     android:orientation="vertical">
 8 
 9     <Button
10         android:id="@+id/btnSmall"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:text="@string/button_small"
14         android:onClick="doSmall"/>
15     <Button
16         android:id="@+id/btnLarge"
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:text="@string/button_large"
20         android:onClick="doLarge"/>
21 
22     <TextView
23         android:id="@+id/txtview"
24         android:text="@string/hello_world"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:capitalize="characters"/>
28 
29 </LinearLayout>
activity_main.xml

 

EventDemo

标签:

原文地址:http://www.cnblogs.com/hello-sandy/p/4707504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!