Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在android中的同一帧布局中获得两个重叠按钮的点击大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个三角形的图像,我想用作按钮背景.
我使用框架布局进行重叠,并能够像@L_675_1@一样制作出理想的形状.

现在我只是在每个按钮上放置onclick监听器,但它不能正常工作,每次只点击按钮两个工作.

请帮我找解决方案.

这是我的@L_617_3@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://scheR_965_11845@as.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中的同一帧布局中获得两个重叠按钮的点击所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。