public class ChatHeadDrawerService extends Service {
private WindowManager windowManager;
private ImageButton chatHead;
@Override public IBinder onBind(Intent intent) {
// Not used
return null;
}
@Override public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageButton(this);
chatHead.setImageResource(R.drawable.x);
chatHead.setOnClickListener(new OnClickListener() {
// Start new list activity
public void onClick(View v) {
Intent mainIntent = new Intent(ChatHeadDrawerService.this,
MainActivity.class);
startActivity(mainIntent);
}
});
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(chatHead, params);
chatHead.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, params);
return true;
}
return false;
}
});
}
点击监听器在聊天头中没有工作但聊天头工作正常并且在屏幕上移动我想在点击聊天头时打开意图感谢帮助.......... ............
这行不起作用
chatHead.setOnClickListener(new OnClickListener() {
// Start new list activity
public void onClick(View v) {
Intent mainIntent = new Intent(ChatHeadDrawerService.this,
MainActivity.class);
startActivity(mainIntent);
}
});
答案 0 :(得分:1)
试试这个
case MotionEvent.ACTION_UP:
if ((Math.abs(initialTouchX - event.getRawX()) < 5) && (Math.abs(initialTouchY - event.getRawY()) < 5)) {
Log.e(TAG, "It's a click!");
} else {
Log.e(TAG, "You moved the head");
}
return true;
答案 1 :(得分:0)
问题在于OnTouchListener。一旦在MotionEvent.ACTION_DOWN事件中返回true,此侦听器就会消耗您的click事件。 阅读此reference
的底线