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的更多相关文章
随机推荐
- [CentOS]CentOS/RedHat/Fedora的Proxy设定(yum,wget,,rpm)
yum 「/etc/yum.conf」 proxy=http://proxy.xxx.com:8080/ wget 「/etc/wgetrc」 http_proxy=http://proxy.xxx. ...
- 记一次Time-Wait导致的问题
去年(2014年)公司决定服务框架改用Finagle(后续文章详细介绍),but 公司业务系统大部分是C#写的,然后 finagle只提供了 scala/java 的Client 于是 只能自己动手丰 ...
- hdu 5258 数长方形 离散化
数长方形 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5258 Des ...
- ExtJS grid tableGrid study
Q: How to color the text in the grid Try: http://dev.sencha.com/playpen/docs/output/Ext.grid.TableG ...
- echarts通过ajax向服务器发送post请求,servlet从数据库读取数据并返回前端
1.echarts的官网上的demo,都是直接写死的随机数据,没有和数据库的交互,所以就自己写了一下,ok,我们开始一步一步走一遍整个流程吧. 就以官网最简单的那个小demo来做修改吧.官网上的小de ...
- [Node.js] Level 7. Persisting Data
Simple Redis Commands Let's start practicing using the redis key-value store from our node applicati ...
- 【CSS系列-选择器优先级总结】
转:http://www.cnblogs.com/dolphinX/p/3511300.html 容易被忽略CSS特性 CSS初学感觉很简单,但随着学习的深入才感觉CSS的水由多深,平常总会遇到各 ...
- 你真的会使用SQL Server的备份还原功能吗?之二:主要备份类型
假设在下面几个时间段中,一个数据库积累插入了如下数据: 1.完整数据库备份 故名思意,完整数据库备份包括完整的数据库信息.它包括数据库的数据文件和备份结尾的部份活动事务日志. 完整备份基本语法如下: ...
- MVC验证01-基础、远程验证
本文体验MVC服务端和客户端验证.主要涉及:※ 基础验证※ 远程验证1个或多个属性及注意点 基础体验 创建MVC4的Internet项目,本身包含了基本的Model,Views,Controller. ...
- IIS问题汇总
1.问题描述 VS和Framework的安装顺序不对导致网站打不开 原因分析 Framework出现问题 解决办法 重新注册Framework版本 a.开始->运行-&g ...