gpt4 book ai didi

java - Android setOnClickListener 一键触发两次

转载 作者:行者123 更新时间:2023-12-02 01:15:05 26 4
gpt4 key购买 nike

我在 mainActivity 的两个 fragment 之一中创建了一个 ImageView“reddot”。在 mainActivity 的 onCreate 部分中,使用 findViewById(R.id.reddot) 找到 ImageView 'reddot' 后,setOnClickListener 被附加到 'RedDot'。但是,每次我单击红点时, toast 都会出现两次。您能帮忙看看我的代码哪里出了问题吗?谢谢!

我尝试将代码排列在其 fragment 中,结果是相同的,单击“reddot”一次,toast 文本将出现两次。我还检查了 ImageView 'reddot' 的 XML 中没有可点击的设置。

“reddot” fragment 的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawContainer"
android:layout_width="match_parent"

android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="38dp"
android:background="@color/white" >


<ImageView
android:id="@+id/reddot"
android:layout_width="21dp"
android:layout_height="21dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="120dp"
android:src="@drawable/red20px"
android:adjustViewBounds="true"
android:scaleType="fitXY" />

</RelativeLayout>

</LinearLayout>

mainActivity的onCreate中的代码:

ImageView reddot = (ImageView) findViewById(R.id.reddot);

reddot.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String redMsg = "red dot";
Toast.makeText(getApplicationContext(),
redMsg, Toast.LENGTH_SHORT).show();

}
}
);

每次点击红点时,Logcat 中的警告消息如下,例如: “W/NotificationService:Toast 已被终止。pkg=com.example.application callback=android.app.ITransientNotification$Stub$Proxy@f4ef1bd”:

2019-11-10 00:28:01.190 1631-3134/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 24257815 , only wrote 24257520
2019-11-10 00:28:02.529 1631-3134/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 24321795 , only wrote 24321600
2019-11-10 00:28:03.838 1631-3134/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 24384412 , only wrote 24384240
2019-11-10 00:28:04.409 1631-1730/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 24438504 , only wrote 24411600

--------- beginning of system
2019-11-10 00:28:04.435 1905-4496/? W/NotificationService: Toast already killed. pkg=com.example.application callback=android.app.ITransientNotification$Stub$Proxy@f4ef1bd
2019-11-10 00:28:04.983 1745-2227/? W/SurfaceFlinger: Attempting to destroy on removed layer: 4f1404e Toast#0

最佳答案

您不能在主 Activity 中的fragment View 上设置onClick()。您可以在fragment的onCreateView()中使用这段代码(红点所在的fragment):

ImageView reddot = (ImageView) getView.findViewById(R.id.reddot);

reddot.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String redMsg = "red dot";
Toast.makeText(getContext(),redMsg, Toast.LENGTH_SHORT).show();

}
}
);

在返回 fragment View 之前编写此代码。

关于java - Android setOnClickListener 一键触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58784837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com