EventDemoandStyleDemoandThemeDemo
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类中使用匿名内部类
- package com.example.shad_fnst.eventdemo;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button buttonSmall = (Button) findViewById(R.id.btnSmall);
- Button buttonLarge = (Button) findViewById(R.id.btnLarge);
- buttonSmall.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- TextView textView = (TextView) findViewById(R.id.txtview);
- textView.setTextSize(24);
- textView.setAllCaps(true);
- }
- });
- buttonLarge.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- TextView textView = (TextView) findViewById(R.id.txtview);
- textView.setTextSize(40);
- textView.setTextColor(0xff32cebb);
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
MainActivity.java
2) 在Activity类中实现Listener接口
- package com.example.shad_fnst.eventdemo;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends ActionBarActivity implements View.OnClickListener{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button buttonSmall = (Button) findViewById(R.id.btnSmall);
- Button buttonLarge = (Button) findViewById(R.id.btnLarge);
- buttonSmall.setOnClickListener(this);
- buttonLarge.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if(v == findViewById(R.id.btnSmall)){
- TextView textView = (TextView) findViewById(R.id.txtview);
- textView.setTextSize(24);
- textView.setAllCaps(true);
- return;
- }
- if(v.getId() == R.id.btnLarge){
- TextView textView = (TextView) findViewById(R.id.txtview);
- textView.setTextSize(40);
- textView.setTextColor(0xff32cebb);
- return;
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
MainActivity.java
方法1和方法2的xml文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
- android:orientation="vertical">
- <Button
- android:id="@+id/btnSmall"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/button_small"/>
- <Button
- android:id="@+id/btnLarge"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/button_large"/>
- <TextView
- android:id="@+id/txtview"
- android:text="@string/hello_world"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:capitalize="characters"/>
- </LinearLayout>
main_activity.xml
3) 使用layout文件activity_main.xml直接实现
- package com.example.shad_fnst.eventdemo;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends ActionBarActivity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void doSmall(View view){
- TextView textView = (TextView) findViewById(R.id.txtview);
- textView.setTextSize(24);
- textView.setAllCaps(true);
- }
- public void doLarge(View view){
- TextView textView = (TextView) findViewById(R.id.txtview);
- textView.setTextSize(40);
- textView.setTextColor(0xff32cebb);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
MainActivity.java
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
- android:orientation="vertical">
- <Button
- android:id="@+id/btnSmall"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/button_small"
- android:onClick="doSmall"/>
- <Button
- android:id="@+id/btnLarge"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/button_large"
- android:onClick="doLarge"/>
- <TextView
- android:id="@+id/txtview"
- android:text="@string/hello_world"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:capitalize="characters"/>
- </LinearLayout>
activity_main.xml
Style示例:
其他文件同上,修改styles.xml和activity_main.xml
- <resources>
- <!-- Base application theme. -->
- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
- <!-- Customize your theme here. -->
- </style>
- <style name="CustomButtonStyle">
- <item name="android:layout_width">100dp</item>
- <item name="android:layout_height">38dp</item>
- <item name="android:capitalize">characters</item>
- <item name="android:typeface">monospace</item>
- <item name="android:shadowDx">1.2</item>
- <item name="android:shadowDy">1.2</item>
- <item name="android:shadowRadius">2</item>
- <item name="android:textColor">#494948</item>
- <item name="android:gravity">center</item>
- <item name="android:layout_margin">3dp</item>
- <item name="android:textSize">5pt</item>
- <item name="android:shadowColor">#000000</item>
- </style>
- <style name="CustomButtonStyle.BigButton">
- <item name="android:layout_width">200dp</item>
- <item name="android:layout_height">76dp</item>
- <item name="android:textSize">10pt</item>
- </style>
- </resources>
Styles.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
- android:orientation="vertical">
- <Button
- android:id="@+id/btnSmall"
- style="@style/CustomButtonStyle"
- android:text="@string/button_small"
- android:onClick="doSmall"/>
- <Button
- android:id="@+id/btnLarge"
- style="@style/CustomButtonStyle.BigButton"
- android:text="@string/button_large"
- android:onClick="doLarge"/>
- <TextView
- android:id="@+id/txtview"
- android:text="@string/hello_world"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:capitalize="characters"/>
- </LinearLayout>
activity_main.xml
Theme示例:
其他文件同上,修改styles.xml,在文件AndroidManifest.xml里配置Theme,可以在application标签,也可以在activity标签。
- <resources>
- <!-- Base application theme. -->
- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
- <!-- Customize your theme here. -->
- <item name="android:capitalize">characters</item>
- <item name="android:typeface">monospace</item>
- <item name="android:shadowDx">1.2</item>
- <item name="android:shadowDy">1.2</item>
- <item name="android:shadowRadius">2</item>
- <item name="android:textColor">#494948</item>
- <item name="android:gravity">center</item>
- <item name="android:layout_margin">3dp</item>
- <item name="android:textSize">5pt</item>
- <item name="android:shadowColor">#000000</item>
- </style>
- <style name="CustomButtonStyle">
- <item name="android:layout_width">100dp</item>
- <item name="android:layout_height">38dp</item>
- </style>
- <style name="CustomButtonStyle.BigButton">
- <item name="android:layout_width">200dp</item>
- <item name="android:layout_height">76dp</item>
- <item name="android:textSize">10pt</item>
- </style>
- </resources>
Styles.xml
EventDemoandStyleDemoandThemeDemo的更多相关文章
随机推荐
- 从jQuery的缓存到事件监听
不知道大家有没有发现,用jQuery选择器"选择"之后的DOM上会添加jQuery*********属性. <DIV id=d1 jQuery1294122065250=&q ...
- Looksery Cup 2015 H. Degenerate Matrix 数学
H. Degenerate Matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/ ...
- 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 ...
- openldap 安装 配置 使用
1.安装 #安装 yum install -y openldap-servers openldap-clients openldap-devel 2.复制配置文件 #复制配置文件 cp /usr/sh ...
- 浅谈模块化的JavaScript
模块化JavaScript之风早已席卷而来, CommonJS . AMD . NodeJS .RequireJS . SeaJS . curljs 等模块化的JavaScript概念及库扑面而来, ...
- iOS开发——UI篇Swift篇&UIActivityIndicatorView
UIActivityIndicatorView override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleStr ...
- h5-2
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 基于jQuery右侧带缩略图导航的焦点图
今天我们要来分享一款右侧带缩略图导航的jQuery焦点图插件,这款jQuery焦点图插件的特点是右侧有一列缩略图导航列表,并且可以定义任意数量的图片,你可以拖动列表来查看所有的图片,点击缩略图后,即可 ...
- Tomcat7启动报Error listenerStart错误--转载
原文地址:http://www.cnblogs.com/nayitian/p/3439336.html 问题 Tomcat7在启动时报错,详细信息如下: 十一月 23, 2013 7:21:58 下午 ...
- 编译linux内核以及depmod的使用
转载:http://blog.lmtw.com/b/18215/archives/2010/71074.html depmod(depend module) 功能说明:分析可载入模块的相依性. 语 法 ...