按钮长按不能正常工作

时间:2018-06-08 13:37:36

标签: button onlongclicklistener

我的应用程序有一个图像,当我点击按钮时,视频开始播放。 我想在按钮长按一下后将视频1更改为视频2,但它不起作用。 当应用程序启动时,请阅读共享首选项以了解最后的首选项。该按钮处于全屏状态。当视频正确启动时按下按钮,完成后它会正确停止。但我不能做长按钮。

这是代码:

VideoView videoview;
    ImageView immagine;
    Button button;
    Integer flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final SharedPreferences prefs = this.getSharedPreferences("general_settings", Context.MODE_PRIVATE);
        String scelta = prefs.getString("scelta", null);

        if (scelta != "videoA" || scelta != "videoB") {
            scelta = "videoA";
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("scelta", "videoA");
            editor.commit();}

        button = (Button) findViewById(R.id.button);
        button.setLongClickable(true);
        immagine = (ImageView) findViewById(R.id.imageView2);
        videoview = (VideoView) findViewById(R.id.videoView);
        videoview.setVisibility(View.GONE);

        if (scelta == "videoA") {
            Picasso.get().load(R.drawable.magnumbkg).resize(1920, 1080)
                .centerCrop().into(immagine);
            String path = "android.resource://" + getPackageName() + "/" + R.raw.magnum;
            videoview.setVideoURI(Uri.parse(path));}

        if (scelta == "videoB") {
            Picasso.get().load(R.drawable.thorbkg).resize(1920, 1080)
                .centerCrop().into(immagine);
            String path = "android.resource://" + getPackageName() + "/" + R.raw.thor;
            videoview.setVideoURI(Uri.parse(path));}

        videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                videoview.stopPlayback();
                videoview.setVisibility(View.GONE);
                immagine.setVisibility(View.VISIBLE);
                flag = 0;
            }
        });

        videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                return true;
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                videoview.start();
                videoview.setVisibility(View.VISIBLE);
                immagine.setVisibility(View.GONE);

            }
        });

        button.setOnLongClickListener(
                new Button.OnLongClickListener() {
                    public boolean onLongClick (View V){
                        Intent i = new Intent(activity_main.this, activity_main.class);
                        String scelta2 = prefs.getString("scelta", null);
                        if (scelta2 == "videoA") {
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.putString("scelta", "videoB");
                            editor.commit();
                            startActivity(i);
                            finish();
                        } else if (scelta2 == "videoB") {
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.putString("scelta", "videoA");
                            editor.commit();
                            startActivity(i);
                            finish();
                        }
                        return true;

                    }
                }
        );

    }

}

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity_main">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:longClickable="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

我不知道我错了什么,所以我希望有人可以帮助我。

0 个答案:

没有答案