
我使用框架布局进行重叠,并能够像图片一样制作出理想的形状.
现在我只是在每个按钮上放置onclick监听器,但它不能正常工作,每次只点击按钮两个工作.
请帮我找到解决方案.
这是我的代码mainActivity.java代码.
package com.example.buttontest;import java.util.Locale;import androID.app.Activity;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.GestureDetector;import androID.vIEw.GestureDetector.OnGestureListener;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OntouchListener;import androID.Widget.button;import androID.Widget.Toast;public class MainActivity extends Activity implements OnGestureListener{ private GestureDetector gestureDetector; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); gestureDetector=new GestureDetector(this); button b1=(button)findVIEwByID(R.ID.button2); button b2=(button)findVIEwByID(R.ID.button1); b1.setontouchListener(new OntouchListener() { public boolean ontouch(VIEw v,MotionEvent e) { int wIDth = findVIEwByID(R.ID.button1).getWIDth(); int height = findVIEwByID(R.ID.button1).getHeight(); float touchedX = e.getX(); float touchedY = e.getY(); boolean inUpperTriangle = (height - touchedY) * wIDth > touchedX * height; if(inUpperTriangle==true) Toast.makeText(getApplicationContext(),"upper",Toast.LENGTH_SHORT).show(); if(inUpperTriangle) gestureDetector.ontouchEvent(e); return inUpperTriangle; } }); b2.setontouchListener(new OntouchListener() { public boolean ontouch(VIEw v,MotionEvent e) { int wIDth = findVIEwByID(R.ID.button2).getWIDth(); int height = findVIEwByID(R.ID.button2).getHeight(); float touchedX = e.getX(); float touchedY = e.getY(); boolean inLowerTriangle = (height - touchedY) * wIDth < touchedX * height; if(inLowerTriangle==true) Toast.makeText(getApplicationContext(),"lower",Toast.LENGTH_SHORT).show(); if(inLowerTriangle) gestureDetector.ontouchEvent(e); return inLowerTriangle; } }); } public boolean onDown(MotionEvent e) { // Todo auto-generated method stub return false; } public boolean onFling(MotionEvent e1,MotionEvent e2,float veLocityX,float veLocityY) { // Todo auto-generated method stub return false; } public voID onLongPress(MotionEvent e) { // Todo auto-generated method stub } public boolean onScroll(MotionEvent e1,float distanceX,float distanceY) { // Todo auto-generated method stub return false; } public voID onShowPress(MotionEvent e) { // Todo auto-generated method stub } public boolean onSingleTapUp(MotionEvent e) { return true; }} 我的布局文件是.
<FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:ID="@+ID/FrameLayout1"androID:layout_wIDth="fill_parent"androID:layout_height="fill_parent" ><button androID:ID="@+ID/button2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:background="@drawable/button2" /><button androID:ID="@+ID/button1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:background="@drawable/button1" />
请告诉我如何正确点击这两个按钮.
这是我的drawables.i使用黄色三角形作为按钮1图像,绿色作为按钮2图像.
解决方法 我会使用touchListener而不是单击侦听器.如果当前视图没有用,touchListener可以检查事件并将其传递给下一个VIEw.那么,做以下事情:
>在每个按钮上注册touchListener.
>在监听器中,检查触摸区域是否在三角形内.如果是这样,请使用该事件(例如显示Toast)并返回true.如果不是,则在侦听器中返回false.
到现在为止还挺好.实现touchListener比实现点击事件要复杂一些.请参阅this answer about detecting taps,其中我添加了一些简短的示例源,用于实现一个简单的touchListener,它可以检测单击(有效点击)和其他手势,并检测用户实际触摸的位置.
在您的情况下,您只需创建两个侦听器,如链接中的侦听器,并将它们链接到两个侦听器.
findVIEwByID(R.ID.button1).setontouchListener(new OntouchListener() {...});findVIEwByID(R.ID.button2).setontouchListener(new OntouchListener() {...}); 编辑:通过一些数学运算,您可以轻松找出实际触摸的三角形.
以下是使用touchListeners实现的代码段:
public boolean ontouch(VIEw v,MotionEvent event) { int wIDth = findVIEwByID(R.ID.button1).getWIDth(); int height = findVIEwByID(R.ID.button1).getHeight(); float touchedX = event.getX(); float touchedY = event.getY(); boolean inUpperTriangle = (height - touchedY) * wIDth > touchedX * height; if(inUpperTriangle) gestureDetector.ontouchEvent(event); return inUpperTriangle;} 使用此ontouch方法将ontouch表单替换为此链接中的源代码.为第二个按钮创建相同的方法,只需在下三角形上做出反应.然后,您可以检测手势监听器的onSingleTapUp方法,如果按钮,监听器被链接到触摸并对其作出反应.
请注意,VIEw的左上角有坐标(0,0),y轴向下增加.这就是(高度触摸Y)的原因.
总结以上是内存溢出为你收集整理的如何在android中的同一帧布局中获得两个重叠按钮的点击全部内容,希望文章能够帮你解决如何在android中的同一帧布局中获得两个重叠按钮的点击所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)