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的更多相关文章
随机推荐
- 【转】Android开发实践:自定义带消息循环(Looper)的工作线程
http://ticktick.blog.51cto.com/823160/1565272 上一篇文章提到了Android系统的UI线程是一种带消息循环(Looper)机制的线程,同时Android也 ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
- Linux文件时间属性
Linux文件时间属性 ...
- DataInputStream和DataOutputStream使用方法细节探讨
DataInputStream和DataOutputStream都是Java中输入输出流的装饰类,用起来非常方便.今天就来讨论一下使用该类时候遇到的编码问题. package com.vince ...
- mysql开启慢查询方法(转)
1,配置开启 Linux: 在mysql配置文件my.cnf中增加 log-slow-queries=/var/lib/mysql/slowquery.log (指定日志文件存放位置,可以为空,系统会 ...
- oc-27-@property的参数
//01加强-10 @property .4前 ) @property + 手动实现 ) @property int age; + @synthesize age;//get和set方法的声明和实现都 ...
- vm内核参数优化设置
http://www.cnblogs.com/wjoyxt/archive/2014/06/08/3777042.html (1)vm.overcommit_memory 执行grep -i com ...
- MOGODB REDIS
http://www.cnblogs.com/huangxincheng/ http://blog.csdn.net/opens_tym/article/details/9832301 http:// ...
- #import与@class的区别
转自:http://www.cnblogs.com/jqyp/archive/2012/01/13/2321707.html 1.import会包含这个类的所有信息,包括实体变量和方法,而@class ...
- Golang学习 - io 包
------------------------------------------------------------ 先说一下接口,Go 语言中的接口很简单,在 Go 语言的 io 包中有这样一个 ...