简体   繁体   English

android 中的弹出菜单未关闭?

[英]Pop up menu not closing in android?

So the problem is as following: I want a pop up menu which opens while pressing the specific button.所以问题如下:我想要一个弹出菜单,在按下特定按钮时打开。 To navigate to another Activity, this button should be dragged onto another one on the screen.要导航到另一个活动,应将此按钮拖到屏幕上的另一个活动上。 The Function worked until I implemented a feature that should close the pop up menu again after navigating to an other activity. Function 一直工作,直到我实现了一个功能,该功能应该在导航到其他活动后再次关闭弹出菜单。 After this, the Activity doesnt function anymore and just the button works.在此之后,活动不再 function 并且按钮工作。 I am a bit confused and would appreciate if anyone could help me with the following code i have written.我有点困惑,如果有人可以帮助我编写我编写的以下代码,我将不胜感激。 Is there any chance that this works and does anyone of you have a solution to fix this problem?这有没有可能有效,你们中的任何人都有解决这个问题的方法吗?

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GestureDetectorCompat;

import android.annotation.SuppressLint;
import android.app.RemoteInput;
import android.content.Intent;
import android.graphics.Rect;
import android.media.effect.Effect;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private ImageView imageViewPlayButton, imageView2, imageView3, imageView4;
    private Rect imageRect;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        detectorCompat = new GestureDetectorCompat(this, new GestureListener());
        imageViewPlayButton = findViewById(R.id.PlayButton_Image_White_Base);
        imageView2 = findViewById(R.id.register_Icon_Base);
        imageView3 = findViewById(R.id.navigation_Background_Image_Register);
        imageView4 = findViewById(R.id.Login_Icon_Base);

        imageViewPlayButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    imageView2.setVisibility(View.VISIBLE);
                    imageView3.setVisibility(View.VISIBLE);
                    imageView4.setVisibility(View.VISIBLE);
                } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageView2.setVisibility(View.INVISIBLE);
                    imageView3.setVisibility(View.INVISIBLE);
                    imageView4.setVisibility(View.INVISIBLE);
                }
                return false;
            }
        });

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (imageRect == null) {
            imageRect = new Rect();
            imageView2.getGlobalVisibleRect(imageRect);
        }
        int x = (int) event.getX();
        int y = (int) event.getY();
        if (imageRect.contains(x, y)) {
            openActivity();
        }

        return true;
    }

    private void openActivity() {
        Intent intent = new Intent(this, Register.class);
        startActivity(intent);
        overridePendingTransition(0, 0);
    }

Maybe you are forgetting to use the dismiss() method when you want your PopupMenu to be closed.当您希望关闭PopupMenu时,您可能忘记使用dismiss()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM