【问题标题】:Imagebutton needs two clicks to works [closed]Imagebutton 需要两次点击才能工作[关闭]
【发布时间】:2020-12-07 22:28:35
【问题描述】:

在我的应用程序中,每个 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();
            }
        });
   }
}

【问题讨论】:

  • “错字”:点击监听器正在设置另一个点击监听器

标签: java android button imagebutton


【解决方案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();
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-15
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多