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 }
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 }
方法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>
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 }
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>
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>
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>
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>
浙公网安备 33010602011771号