七、键盘事件

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地址:" /><EditTextandroid:id="@+id/input"android:layout_width="wrap_content"android:layout_height="wrap_content"android:selectAllOnFocus="true" /><ImageViewandroid: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. */@Overridepublic 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("\\[email protected]\\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.MyPaintViewandroid: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:[email protected] *  * @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; // 表示下面的操作不再执行了。}}@Overrideprotected 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);}}}}}

更多相关文章

  1. Android之四大组件
  2. Android的TextView组件相关属性
  3. Android 软键盘弹出时布局位置改变
  4. android GridView item中组件获取焦点
  5. Android架构组件(三)——ViewModel
  6. [android]editText和软键盘的一些总结

随机推荐

  1. 编译android4.0.4 webcore_test报错
  2. Android(安卓)studio简易计算机
  3. Android程序猿来搭建服务器
  4. Android声音焦点----从音乐回到Luncher调
  5. dd
  6. Android(安卓)Studio 多渠道打包(二) ---
  7. Android(安卓)ANR keyDispatchingTimedOu
  8. Please ensure that adb is correctly lo
  9. android 读取 attr 资源
  10. 搜集的android资源