1.長押しイベント
1.1 知識ポイント
(1)長押しイベントの操作フォームをマスターする。
(2) 携帯電話のデスクトップの背景を設定できます。
1.2 具体内容
例: 画像を長押しすると、その画像が携帯電話のデスクトップの背景として設定されます。
<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: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=".LongClickActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长按图片设置桌面背景" />
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/head11"
/>
</LinearLayout>
package com.example.longclickproject;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
public class LongClickActivity extends Activity {
ImageView img =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_long_click);
img = (ImageView) super.findViewById(R.id.img);
img.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
try {
LongClickActivity.this.clearWallpaper();
LongClickActivity.this.setWallpaper(LongClickActivity.this.img.getResources()
.openRawResource(R.drawable.head11));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//清除桌面背景
return false;
}
});
}
}
上記のプログラムは長押しすると直接エラーを報告しますが、これはデスクトップの背景の設定が携帯電話のシステムに関わる操作であるためであり、対応する権限が設定されていないとそのような操作は実行できませんので、設定する必要があります。メインの設定ファイル。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.longclickproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission
android:name="android.permission.SET_WALLPAPER"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.longclickproject.LongClickActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
再度実行したところ、プログラムが対応する作業を正常に完了できることがわかりました。
1.3 概要
(1) 長押しイベントは、トリガーされてから 2 秒後にのみ応答します。
(2) デスクトップ操作方法:
デスクトップをクリアします: public void clearWallpaper()
デスクトップを設定します: public void setWallpaper(InputStream data)
2. キーボードイベント
2.1 知識ポイント
(1) キーボードイベントの使い方をマスターする。
(2) キーボード イベントは EMAIL 検証に使用できます。
2.2 具体内容
例: 電子メール認証を実行する
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".OnKeyListenerActivity" >
<EditText
android:id="@+id/edt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
package com.example.onkeylistenerproject;
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.TextView;
public class OnKeyListenerActivity extends Activity {
EditText edt = null;
TextView tv =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_key_listener);
edt = (EditText) super.findViewById(R.id.edt);
tv = (TextView) super.findViewById(R.id.tv);
edt.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
String email = edt.getText().toString();
if(event.getAction()==KeyEvent.ACTION_UP){//键盘松开
if(email.matches("\\w+@\\w+(.com|.cn|.org|.edu)")){//进行正则的验证
tv.setText("输入的为正确的邮箱格式");
}else{
tv.setText("输入的邮箱格式有问题");
}
}
return false;
}
});
}
}
2.3 概要
(1) 正規表現を入力コンポーネントで使用してデータ入力を検証できます。
(2) キーボード イベントは、ユーザーがデータを入力したときにトリガーされる操作です。
3.タッチイベント
3.1 知識ポイント
(1) タッチイベントの基本的な操作形態を理解する。
(2) タッチイベントを利用して基本的な描画操作を行うことができます。
3.2 具体内容
もちろん、OnTouchListener 自体も Android が提供するリスニング インターフェイスであり、実際の Android 開発にはあまり役に立たず、一般的にゲーム開発に適しています。
例: タッチポイントの座標を取得します。
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".OnTocuhListenerActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
package com.example.ontouchlistenerproject;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
public class OnTocuhListenerActivity extends Activity {
TextView tv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_tocuh_listener);
tv = (TextView) super.findViewById(R.id.tv);
tv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
tv.setText("X轴:"+x+",Y轴:"+y);
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.on_tocuh_listener, menu);
return true;
}
}
OnTouch イベントに、タッチして画面に線を描くなど、より実用的な効果を与えたい場合は、いくつかの描画操作を通じて実行できますが、描画操作を完了したい場合は、新しいコンポーネントをカスタマイズする必要があります。コンポーネントクラスなのでViewクラスを継承する必要があります。
漠然
package com.example.myView;
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;
public class MyView extends View {
private List<Point> allPoint = new ArrayList<Point>();
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//在实例化这个组件的同时就进行触摸事件的监听
super.setOnTouchListener(new OnTouchListenerImpl());
}
protected void onDraw(Canvas canvas){
Paint p = new Paint();//画线依靠此类,相当于画笔类
p.setColor(Color.RED);//设置画笔颜色
if(MyView.this.allPoint.size()>0){//如果存在坐标点的话,就可以开始绘制图形
Iterator<Point> it = MyView.this.allPoint.iterator();
Point frist = null;
Point last = null;
while(it.hasNext()){
if(null==frist){
frist = it.next();
}else{
if(last!=null){//当一段线段接收之后,下端开始的点将会是本段中结束的点
frist = last;
}
last = it.next();
canvas.drawLine(frist.x, frist.y, last.x, last.y, p);
}
}
}
}
class OnTouchListenerImpl implements OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
Point p = new Point((int)event.getX(),(int)event.getY());
if(event.getAction()==MotionEvent.ACTION_DOWN){//如果是触摸下去,就开始保存坐标
MyView.this.allPoint.add(p);
}else if(event.getAction()==MotionEvent.ACTION_UP){//如果手指松开,进行画线
MyView.this.allPoint.add(p);
MyView.this.postInvalidate();//重新绘制图形
}else if(event.getAction()==MotionEvent.ACTION_MOVE){//手指一动
MyView.this.allPoint.add(p);
MyView.this.postInvalidate();//重新绘制图形
}
return true;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.myView.MyView
android:id="@+id/mv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
package com.example.ontouchlistenerproject;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
public class OnTocuhListenerActivity extends Activity {
TextView tv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myview_layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.on_tocuh_listener, menu);
return true;
}
}
3.3 概要
(1) タッチイベントは、ユーザーが画面に触れた後に生成されるイベントです。
(2) ユーザーがタッチ イベントを使用してグラフィックを描画したい場合は、カスタム描画コンポーネントを作成する必要があります。