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

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. public class MainActivity extends ActionBarActivity implements View.OnClickListener{
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17.  
  18. Button buttonSmall = (Button) findViewById(R.id.btnSmall);
  19. Button buttonLarge = (Button) findViewById(R.id.btnLarge);
  20.  
  21. buttonSmall.setOnClickListener(this);
  22. buttonLarge.setOnClickListener(this);
  23. }
  24.  
  25. @Override
  26. public void onClick(View v) {
  27. if(v == findViewById(R.id.btnSmall)){
  28. TextView textView = (TextView) findViewById(R.id.txtview);
  29. textView.setTextSize(24);
  30. textView.setAllCaps(true);
  31. return;
  32. }
  33. if(v.getId() == R.id.btnLarge){
  34. TextView textView = (TextView) findViewById(R.id.txtview);
  35. textView.setTextSize(40);
  36. textView.setTextColor(0xff32cebb);
  37. return;
  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

方法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. public class MainActivity extends ActionBarActivity{
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. }
  18.  
  19. public void doSmall(View view){
  20. TextView textView = (TextView) findViewById(R.id.txtview);
  21. textView.setTextSize(24);
  22. textView.setAllCaps(true);
  23. }
  24.  
  25. public void doLarge(View view){
  26. TextView textView = (TextView) findViewById(R.id.txtview);
  27. textView.setTextSize(40);
  28. textView.setTextColor(0xff32cebb);
  29. }
  30.  
  31. @Override
  32. public boolean onCreateOptionsMenu(Menu menu) {
  33. // Inflate the menu; this adds items to the action bar if it is present.
  34. getMenuInflater().inflate(R.menu.menu_main, menu);
  35. return true;
  36. }
  37.  
  38. @Override
  39. public boolean onOptionsItemSelected(MenuItem item) {
  40. // Handle action bar item clicks here. The action bar will
  41. // automatically handle clicks on the Home/Up button, so long
  42. // as you specify a parent activity in AndroidManifest.xml.
  43. int id = item.getItemId();
  44.  
  45. //noinspection SimplifiableIfStatement
  46. if (id == R.id.action_settings) {
  47. return true;
  48. }
  49.  
  50. return super.onOptionsItemSelected(item);
  51. }
  52. }

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

Style示例:

其他文件同上,修改styles.xml和activity_main.xml

  1. <resources>
  2.  
  3. <!-- Base application theme. -->
  4. <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  5. <!-- Customize your theme here. -->
  6. </style>
  7.  
  8. <style name="CustomButtonStyle">
  9. <item name="android:layout_width">100dp</item>
  10. <item name="android:layout_height">38dp</item>
  11. <item name="android:capitalize">characters</item>
  12. <item name="android:typeface">monospace</item>
  13. <item name="android:shadowDx">1.2</item>
  14. <item name="android:shadowDy">1.2</item>
  15. <item name="android:shadowRadius">2</item>
  16. <item name="android:textColor">#494948</item>
  17. <item name="android:gravity">center</item>
  18. <item name="android:layout_margin">3dp</item>
  19. <item name="android:textSize">5pt</item>
  20. <item name="android:shadowColor">#000000</item>
  21. </style>
  22.  
  23. <style name="CustomButtonStyle.BigButton">
  24. <item name="android:layout_width">200dp</item>
  25. <item name="android:layout_height">76dp</item>
  26. <item name="android:textSize">10pt</item>
  27. </style>
  28.  
  29. </resources>

Styles.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. style="@style/CustomButtonStyle"
  12. android:text="@string/button_small"
  13. android:onClick="doSmall"/>
  14. <Button
  15. android:id="@+id/btnLarge"
  16. style="@style/CustomButtonStyle.BigButton"
  17. android:text="@string/button_large"
  18. android:onClick="doLarge"/>
  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>

activity_main.xml

Theme示例:

其他文件同上,修改styles.xml,在文件AndroidManifest.xml里配置Theme,可以在application标签,也可以在activity标签。

  1. <resources>
  2.  
  3. <!-- Base application theme. -->
  4. <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  5. <!-- Customize your theme here. -->
  6. <item name="android:capitalize">characters</item>
  7. <item name="android:typeface">monospace</item>
  8. <item name="android:shadowDx">1.2</item>
  9. <item name="android:shadowDy">1.2</item>
  10. <item name="android:shadowRadius">2</item>
  11. <item name="android:textColor">#494948</item>
  12. <item name="android:gravity">center</item>
  13. <item name="android:layout_margin">3dp</item>
  14. <item name="android:textSize">5pt</item>
  15. <item name="android:shadowColor">#000000</item>
  16. </style>
  17.  
  18. <style name="CustomButtonStyle">
  19. <item name="android:layout_width">100dp</item>
  20. <item name="android:layout_height">38dp</item>
  21. </style>
  22.  
  23. <style name="CustomButtonStyle.BigButton">
  24. <item name="android:layout_width">200dp</item>
  25. <item name="android:layout_height">76dp</item>
  26. <item name="android:textSize">10pt</item>
  27. </style>
  28.  
  29. </resources>

Styles.xml

EventDemoandStyleDemoandThemeDemo的更多相关文章

随机推荐

  1. 从jQuery的缓存到事件监听

    不知道大家有没有发现,用jQuery选择器"选择"之后的DOM上会添加jQuery*********属性. <DIV id=d1 jQuery1294122065250=&q ...

  2. Looksery Cup 2015 H. Degenerate Matrix 数学

    H. Degenerate Matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/ ...

  3. hdu 4497 GCD and LCM 数学

    GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4 ...

  4. openldap 安装 配置 使用

    1.安装 #安装 yum install -y openldap-servers openldap-clients openldap-devel 2.复制配置文件 #复制配置文件 cp /usr/sh ...

  5. 浅谈模块化的JavaScript

    模块化JavaScript之风早已席卷而来, CommonJS . AMD . NodeJS .RequireJS . SeaJS . curljs  等模块化的JavaScript概念及库扑面而来, ...

  6. iOS开发——UI篇Swift篇&UIActivityIndicatorView

    UIActivityIndicatorView override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleStr ...

  7. h5-2

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. 基于jQuery右侧带缩略图导航的焦点图

    今天我们要来分享一款右侧带缩略图导航的jQuery焦点图插件,这款jQuery焦点图插件的特点是右侧有一列缩略图导航列表,并且可以定义任意数量的图片,你可以拖动列表来查看所有的图片,点击缩略图后,即可 ...

  9. Tomcat7启动报Error listenerStart错误--转载

    原文地址:http://www.cnblogs.com/nayitian/p/3439336.html 问题 Tomcat7在启动时报错,详细信息如下: 十一月 23, 2013 7:21:58 下午 ...

  10. 编译linux内核以及depmod的使用

    转载:http://blog.lmtw.com/b/18215/archives/2010/71074.html depmod(depend module) 功能说明:分析可载入模块的相依性. 语 法 ...