七、键盘事件
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入EMAIL地址:" /> <EditText android:id="@+id/input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:selectAllOnFocus="true" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/wrong"/> </LinearLayout>
Activity:
package com.iflytek.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ImageView;
public class EventActivity extends Activity {
private EditText input = null;
private ImageView img = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.input = (EditText) super.findViewById(R.id.input); // 取得组件
this.img = (ImageView) super.findViewById(R.id.img); // 取得组件
this.input.setOnKeyListener(new OnKeyListenerImpl());
}
private class OnKeyListenerImpl implements OnKeyListener {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (event.getAction()) {
case KeyEvent.ACTION_UP:
String msg = EventActivity.this.input.getText().toString(); // 取得输入的文字信息
if (msg.matches("\\w+@\\w+\\.\\w+")) { // 验证通过
EventActivity.this.img.setImageResource(R.drawable.right); // 设置正确图片
} else {
EventActivity.this.img.setImageResource(R.drawable.wrong); // 设置错误图片
}
case KeyEvent.ACTION_DOWN: // 键盘按下
break;
}
return false;
}
}
}
八、触摸事件
Demo01_ 获取坐标 :
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/info" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
Activity:
package com.iflytek.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
public class EventActivity extends Activity {
private TextView info = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.info = (TextView) super.findViewById(R.id.info);
this.info.setOnTouchListener(new OnTouchListenerImpl());
}
private class OnTouchListenerImpl implements OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
EventActivity.this.info.setText("X = " + event.getX() + ",Y = "
+ event.getY()); // 设置文本
return false;
}
}
}
Demo02_ 绘图:
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.iflytek.activity.MyPaintView android:id="@+id/paintView" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
Activity:
package com.iflytek.activity;
import android.app.Activity;
import android.os.Bundle;
public class EventActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
MyPaintView.java:
package com.iflytek.activity;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* @author xdwang
*
* @create 2012-9-3 下午9:46:43
*
* @email:xdwangiflytek@gmail.com
*
* @description
*
*/
public class MyPaintView extends View {
private List<Point> allPoint = new ArrayList<Point>(); // 保存所有的操作坐标
public MyPaintView(Context context, AttributeSet attrs) { // 接收Context,同时接收属性集合
super(context, attrs); // 调用父类的构造
super.setOnTouchListener(new OnTouchListenerImpl());
}
private class OnTouchListenerImpl implements OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
Point p = new Point((int) event.getX(), (int) event.getY()); // 将坐标保存在Point类
if (event.getAction() == MotionEvent.ACTION_DOWN) { // 按下,表示重新开始保存点
MyPaintView.this.allPoint = new ArrayList<Point>(); // 重绘
MyPaintView.this.allPoint.add(p); // 保存点
} else if (event.getAction() == MotionEvent.ACTION_UP) { // 用户松开
MyPaintView.this.allPoint.add(p); // 记录坐标点
MyPaintView.this.postInvalidate(); // 重绘图形
} else if (event.getAction() == MotionEvent.ACTION_MOVE) { // 用户移动
MyPaintView.this.allPoint.add(p); // 记录坐标点
MyPaintView.this.postInvalidate(); // 重绘图形
}
return true; // 表示下面的操作不再执行了。
}
}
@Override
protected void onDraw(Canvas canvas) { // 进行绘图
Paint p = new Paint(); // 依靠此类开始画线
p.setColor(Color.RED);// 定义图的颜色
if (MyPaintView.this.allPoint.size() > 1) { // 现在有坐标点保存的时候可以开始进行绘图
Iterator<Point> iter = MyPaintView.this.allPoint.iterator();
Point first = null;
Point last = null;
while (iter.hasNext()) {
if (first == null) {
first = (Point) iter.next(); // 取出坐标
} else {
if (last != null) { // 前一阶段已经完成了
first = last; // 重新开始下一阶段
}
last = (Point) iter.next(); // 结束点坐标
canvas.drawLine(first.x, first.y, last.x, last.y, p);
}
}
}
}
}