我已经在mainActivity的两个片段之一中创建了一个ImageView "reddot“。在mainActivity的onCreate部分中,使用findViewById(R.id.reddot)找到ImageView 'RedDot‘后,setOnClickListener将连接到'reddot’。但是,每次我单击红点时,吐司都会出现两次。你能帮我看看我的代码哪里出了问题吗?谢谢!
我试着把代码排列在它的片段中,结果是一样的,点击一下'reddot',吐司文本就会出现两次。我还检查了在ImageView 'reddot‘的XML中没有可点击的设置。
其片段的XML代码'reddot‘:
<?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();
}
}
);每次单击reddot时,Logcat中的警告消息如下所示。"W/NotificationService: callback=android.app.ITransientNotification$Stub$Proxy@f4ef1bd“已被删除。pkg=com.example.application already:
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发布于 2019-11-10 13:27:38
您不能在主activity.You中的片段视图上设置onClick(),可以在片段(红点所在的片段)的onCreateView()中使用这段代码:
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的视图之前编写此代码。
发布于 2019-11-10 08:55:02
试试这个。
Toast toast = Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 500);或者,如果这不起作用,您可以使用替代方法(AppMsg、Crouton或新的SnackBar),或者在Activity.onPause()中保留对Toast的引用并取消()它。
发布于 2019-11-10 15:16:54
这个问题可能是因为您在activity中调用了setOnClickListener,而视图却处于片段布局中。
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState)
ImageView reddot = (ImageView) view.findViewById(R.id.reddot);
reddot.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String redMsg = "red dot";
Toast.makeText(getActivity(),
redMsg, Toast.LENGTH_SHORT).show();
}
}
);
}https://stackoverflow.com/questions/58784837
复制相似问题