简体   繁体   中英

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. After this, the Activity doesnt function anymore and just the button works. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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