事件处理(三)
5.焦点事件
et.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
TextView tvv=(TextView) v;
//hasFocues:是否获得焦点
if(hasFocus){
tvv.setText("");
}else{
tv.setText(tvv.getText().toString());
}
}
});
6.长按事件
修改AndroidManifest.xml文件,并增加以下的授权操作
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
iv.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
try {
Day07.this.clearWallpaper();//清除已有的桌面
Day07.this.setWallpaper(
Day07.this.iv.getResources()
.openRawResource(R.drawable.sss));//设置新的桌面背景
tv.setText("成功");
} catch (Exception e) {
tv.setText("失败");
e.printStackTrace();
}
return true;
}
});
7.键盘事件处理
et.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP){
String string=et.getText().toString();
if(string.matches("\\w+[@]\\w+[.]\\w")){
iv.setImageResource(R.drawable.sss);
}else{
iv.setImageResource(R.drawable.icon);
}
}
return false;
}
});
8.触摸事件
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
tv.setText("("+event.getX()+","+event.getY()+")");
return false;
}
});