Java Imagebutton需要单击两次才能工作
在我的应用程序中,每个Imagebutton都需要点击两次才能工作,但普通的按钮只需点击一次就可以正常工作。应用程序的每个活动都会发生这种情况 我遇到的问题是,我需要单击两次项目按钮来启动以下活动。我不想发生这种情况,我只想“点击一下”启动一项活动 以下是Xml代码:Java Imagebutton需要单击两次才能工作,java,android,button,imagebutton,Java,Android,Button,Imagebutton,在我的应用程序中,每个Imagebutton都需要点击两次才能工作,但普通的按钮只需点击一次就可以正常工作。应用程序的每个活动都会发生这种情况 我遇到的问题是,我需要单击两次项目按钮来启动以下活动。我不想发生这种情况,我只想“点击一下”启动一项活动 以下是Xml代码: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLay
<?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>
您正在另一个click listener中定义一个click listener,这就是执行代码需要单击的原因。删除嵌套的侦听器并只保留一个,它将正常工作
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();
}
});
“输入错误”:单击侦听器正在设置另一个单击侦听器
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();
}
});