Imagebutton需要两次点击才能起作用

时间:2020-08-18 17:23:54

标签: java android button imagebutton

在我的应用中,每个Imagebutton都需要单击两次才能起作用,但是“ Normal”按钮只需单击一下即可正常工作。该应用的每项活动都在发生这种情况。

我的问题是我需要单击两次项目按钮以启动以下活动。我不希望发生这种情况,我只想“一键启动”活动。

这是Xml代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Class10_Eng">
<ScrollView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <ImageButton
            android:id="@+id/imgbutton"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:scaleType="fitCenter"
            android:src="@drawable/class10_eng_first_flight"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
          />
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="First Flight"

            />
 </LinearLayout>
   </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

这是主要活动代码

package com.example.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

public class Class10_Eng extends AppCompatActivity {
Button button;
ImageButton imgButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class10__eng);
        imgButton =(ImageButton)findViewById(R.id.imgbutton);
        imgButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String url="url";

                        Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                        Context context = getApplicationContext();
                        CharSequence text = "Download Starting in Background!";
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }
                });

            }
        });
        button = findViewById(R.id.button);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url="url";
                Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                Context context = getApplicationContext();
                CharSequence text = "Download Starting in Background!";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
        });
   }
}

1 个答案:

答案 0 :(得分:1)

您正在另一个点击侦听器中定义一个点击侦听器,这就是点击以执行代码所需要的原因。删除嵌套的侦听器,并只保留一个,它将正常工作。

imgButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url = "url";

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            Context context = getApplicationContext();
            CharSequence text = "Download Starting in Background!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    });
相关问题