实训3 事件监听器
一、【实训目的】
(1) 事件监听三要素
(2) 事件监听常用方法
(3) 事件监听器
二、【实训步骤和要求】
-
实现下图所示的用户界面设计和功能。
1)用类自身实现监听器。单击按钮“MainActivity类自身设置监听器”,在TextView中显示“MainActivity类自身设置监听器”,字体颜色为绿色,加粗。
2)用内部类实现监听器。单击按钮“内部类作为事件监听器”,在TextView中显示“内部类作为事件监听器”,字体颜色为红色,加粗。
3)用外部类实现监听器。单击按钮“外部类作为事件监听器”,在TextView中显示“外部类作为事件监听器”,字体颜色为蓝色,加粗。
4)用匿名内部类实现监听器。单击按钮“匿名内部类作为事件监听器”,在TextView中显示“匿名内部类作为事件监听器”,字体颜色为黄色,加粗。
源代码:
MainActivity.java源代码:
package com.example.tt1;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity implements OnClickListener{
MyListener myListener = new MyListener();
private Button myself,classin,classout,inclassin;
private TextView test;
private int size = 12;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myself=(Button) findViewById(R.id.myself);
classin=(Button) findViewById(R.id.classin);
classout=(Button) findViewById(R.id.classout);
inclassin=(Button) findViewById(R.id.inclassin);
test=(TextView)findViewById(R.id.test);
//外部类作为事件监听器
OutterListener outterListener = new OutterListener(test);
classout.setOnClickListener(outterListener);
//MainActivity类自身设置监听器
myself.setOnClickListener(this);
//内部类作为事件监听器
classin.setOnClickListener(myListener);
// 匿名内部类实现对事件的监听
inclassin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v1) {
switch(v1.getId()){
// TODO Auto-generated method stub
case R.id.inclassin:
test.setTextColor(Color.YELLOW);
test.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
test.setText("匿名内部类作为事件监听器");
break;
default:break;
}
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class MyListener implements OnClickListener {
public void onClick(View v2) {
// TODO Auto-generated method stub
switch(v2.getId()){
case R.id.classin:
test.setTextColor(Color.RED);
test.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
test.setText("内部类作为事件监听器");
break;
default:
break;
}
}
}
@Override
public void onClick(View v3) {
// TODO Auto-generated method stub
switch(v3.getId()){
case R.id.myself:
test.setTextColor(Color.GREEN);
test.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
test.setText("MainActivity类自身设置监听器");
break;
default:break;
}
}
}
OutterListener.java源代码:
package com.example.tt1;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
//事件监听器
public class OutterListener implements OnClickListener {
private TextView test;
OutterListener(TextView textView) {
this.test = textView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.classout:
test.setTextColor(Color.BLUE);
test.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
test.setText("外部类作为事件监听器");
break;
default:
break;
}
}
}
strings.xml源代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">EditTest</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="test">以MainActivity自身设置监听器</string>
<string name="myself">MainActivity类自身设置监听器</string>
<string name="classin">内部类作为事件监听器</string>
<string name="classout">外部类作为事件监听器</string>
<string name="inclassin">匿名内部类作为事件监听器</string>
</resources>
activity_main.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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/test" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/myself"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/myself" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/classin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/classin" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/classout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/classout" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/inclassin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/inclassin" />
</LinearLayout>
</LinearLayout>
项目成果:
2、实现下图所示的用户界面设计和功能。
1)实现颜色的相应变化;
2)实现字体的增大缩小,最大字体78,最小字体4号;
3)实现字体样式的加粗、正常;
源代码:
Activity_main.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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/test" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/color" />
<Button
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/red" />
<Button
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/green" />
<Button
android:id="@+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/blue" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/size" />
<Button
android:id="@+id/bigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bigger" />
<Button
android:id="@+id/smaller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/smaller" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/style" />
<Button
android:id="@+id/bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bold" />
<Button
android:id="@+id/itatic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/itatic" />
<Button
android:id="@+id/normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/normal" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content" />
<EditText
android:id="@+id/content"
android:layout_width="200dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
MainActivity.java源代码:
package com.example.tt2;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button red, blue, green, bigger, smaller, bold, itatic, normal;
private TextView test;
private int size = 12;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
red = (Button) findViewById(R.id.red);
blue = (Button) findViewById(R.id.blue);
green = (Button) findViewById(R.id.green);
bigger = (Button) findViewById(R.id.bigger);
smaller = (Button) findViewById(R.id.smaller);
bold = (Button) findViewById(R.id.bold);
itatic = (Button) findViewById(R.id.itatic);
normal = (Button) findViewById(R.id.normal);
test = (TextView) findViewById(R.id.test);
red.setOnClickListener(this);// 对事件源进行事件监听
blue.setOnClickListener(this);// 对事件源进行事件监听
green.setOnClickListener(this);// 对事件源进行事件监听
bigger.setOnClickListener(this);// 对事件源进行事件监听
smaller.setOnClickListener(this);// 对事件源进行事件监听
bold.setOnClickListener(new OnClickListener() {
public void onClick(View v1) {
// TODO Auto-generated method stub
switch (v1.getId()) {
case R.id.bold:
test.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
break;
default:break;
}
}
});
itatic.setOnClickListener(new OnClickListener() {
public void onClick(View v2) {
// TODO Auto-generated method stub
switch (v2.getId()) {
case R.id.itatic:
test.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
break;
default:break;
}
}
});
normal.setOnClickListener(new OnClickListener() {
public void onClick(View v3) {
// TODO Auto-generated method stub
switch (v3.getId()) {
case R.id.normal:
test.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
break;
default:break;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.red:
test.setTextColor(Color.RED);
break;
case R.id.blue:
test.setTextColor(Color.BLUE);
break;
case R.id.green:
test.setTextColor(Color.GREEN);
break;
case R.id.bigger:
size=(int) test.getTextSize();
size+=4;
if(size>78){
size=78;
}
test.setTextSize(size);
break;
case R.id.smaller:
size=(int) test.getTextSize();
size-=4;
if(size<4){
size=4;
}
test.setTextSize(size);
break;
default:
break;
}
}
}
String.xml源代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">EditTest</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="test">测试文字,测试文字!</string>
<string name="color">颜色</string>
<string name="red">红色</string>
<string name="green">绿色</string>
<string name="blue">蓝色</string>
<string name="size">大小</string>
<string name="bigger">增大</string>
<string name="smaller">缩小</string>
<string name="style">样式</string>
<string name="bold">加粗</string>
<string name="itatic">倾斜</string>
<string name="normal">正常</string>
<string name="content">内容</string>
</resources>
项目成果:
附注:该专栏是博主上学时的实训项目,可供访客练习与参考。代码质量不是很好,但能实现,仅供参考!