问题描述
在我的应用中,每个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,duration);
toast.show();
}
});
}
}
解决方法
您正在另一个点击侦听器中定义一个点击侦听器,这就是点击以执行代码所需要的原因。删除嵌套的侦听器,并只保留一个,它将正常工作。
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();
}
});