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的更多相关文章

随机推荐

  1. 【M35】让自己习惯于标准C++语言

    1.最近一些年C++语言增加的特性有: a.RTTI,namespace,bool,关键字mutable和explicit,enums,以及const static int可以直接初始化. b.扩充了 ...

  2. Qt多线程学习:创建多线程

    [为什么要用多线程?] 传统的图形用户界面应用程序都仅仅有一个运行线程,而且一次仅仅运行一个操作.假设用户从用户界面中调用一个比較耗时的操作,当该操作正在运行时,用户界面一般会冻结而不再响应.这个问题 ...

  3. Eclipse中R文件不能自动生成

       R文件不能自动生成主要是因为编译有错误,这时你想什么办法都是没有用的,clean, fix properties,都不是从根上解决问题.    R文件主要是自动生成资源文件的id的,里边静态子类 ...

  4. oc-27-@property的参数

    //01加强-10 @property .4前 ) @property + 手动实现 ) @property int age; + @synthesize age;//get和set方法的声明和实现都 ...

  5. oc-22-sel

    /** sel: 1.作用:包装方法 2.格式:typedef struct objc_selector *SEL; 3.用法: SEL 名称 = @selector(方法); 调用形式: [对象 p ...

  6. DB2 重新设定表自增字段的当前值

    转自:http://blog.csdn.net/jionghan3855/article/details/2709073 1.ALTER TABLE UKEY_INFO_TAB ALTER COLUM ...

  7. stm32出现错误“identifier file is undefined”

    为什么记录这个问题,说来很简单,这已经是第二次犯这个小错误了. 出现了错误“identifier file is undefined”的解决方法;option->general options- ...

  8. c# 高效的线程安全队列ConcurrentQueue(下) Segment类

    Segment成员变量 long long m_index; 记录该segment的索引号. int* volatile m_state; 状态数组,标识所对应的元素节点的状态,默认值为0,如果该元素 ...

  9. linxu,window系统

    window下:net start VMwareHostdVMAuthdServiceVMUSBArbService"VMware NAT Service"VMnetDHCP #启 ...

  10. JS 公共方法

    1.Format函数 $.GM.Format = function (source, params) { if (arguments.length == 1) return function () { ...