首页 > 代码库 > 事件2

事件2


六. 长按事件
//修改桌面背景需要配置权限
<uses-permission android:name="android.permission.SET_WALLPAPER" />
img.setOnLongClickListener(new OnLongClickListener() {

//两个事件:long--> click
// false --> 不去运行click
// true --> 去运行click
@Override
public boolean onLongClick(View v) {
try {
Day07.this.clearWallpaper();
Day07.this.setWallpaper(
Day07.this.getResources().
openRawResource(R.drawable.a));
tv.setText("设置成功");
} catch (IOException e) {
tv.setText("设置失败");
e.printStackTrace();
}
return true;
}
});


七. 键盘事件
et.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP){
String str = et.getText().toString();
if(str.matches("\\w+[@]\\w+[.]\\w+")){
iv.setImageResource(R.drawable.a);
}else{
iv.setImageResource(R.drawable.icon);
}
}
return false;
}
});

八. 触摸事件
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
tv.setText("("+event.getX()+","+event.getY()+")");
return false;
}
});

事件2