我使用在线教程创建了一个简单的应用程序,它将Imagebutton添加到窗口中.窗口我的意思是Imagebutton显示在其余活动之上.它始终可见.
我不知道如何处理聊天头的点击事件.我设置了一个onClickListener但它没有做任何事情.
这是我的ChatheadService.java
package com.example.test;import androID.app.Service;import androID.content.Intent;import androID.graphics.PixelFormat;import androID.os.IBinder;import androID.vIEw.Gravity;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.WindowManager;import androID.Widget.Imagebutton;import androID.Widget.ImageVIEw;public class ChatheadService extends Service { private WindowManager windowManager; private ImageVIEw 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.ic_launcher); 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.CENTER; params.x = 0; params.y = 100; windowManager.addVIEw(chathead, params); chathead.setClickable(true); chathead.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw arg0) { } }); 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; } }); } @OverrIDe public voID onDestroy() { super.onDestroy(); if (chathead != null) windowManager.removeVIEw(chathead); }}
解决方法:
试试这个 :
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;
总结 以上是内存溢出为你收集整理的android – 聊天头点击事件全部内容,希望文章能够帮你解决android – 聊天头点击事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)