代码之家  ›  专栏  ›  技术社区  ›  Twing90

长按按钮不起作用

  •  0
  • Twing90  · 技术社区  · 6 年前

    我的应用程序有一个图像,当我单击按钮时,一个视频开始。 当应用程序启动时,读取共享的首选项以了解最后的首选项是什么。按钮全屏显示。当我按下按钮,一旦视频开始正确,当它结束时,它停止正确。但我不能长按按钮。

    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 回复  |  直到 6 年前