imagebutton需要点击两下才能工作

qvtsj1bj  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(394)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

5个月前关门了。
改进这个问题
在我的应用程序中,每个imagebutton都需要点击两下才能工作,但是普通的按钮只要点击一下就可以了。应用程序的每个活动都会发生这种情况。
我的问题是,我需要单击两次项目按钮来启动以下活动。我不想发生这种事,我只想“一键”启动一个活动。
以下是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();
            }
        });
   }
}
oaxa6hgo

oaxa6hgo1#

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

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();
        }
    });

相关问题