android-onClick不会在带有子项的LinearLayout上触发
我有一个带有较小TextView子级的自定义LinearLayout。 我希望能够单击TextView未覆盖的区域,因此我将clickable = true和onclicklistener设置为LinearLayout,但不会触发onClick。 如果我在TextView上设置onclick侦听器,它将按预期工作...
有人可以帮忙吗?
ar_item.xml
android:id="@+id/ar_item" android:layout_width="202dp"
android:layout_height="62dp" android:background="@drawable/bg_item_ar"
android:clickable="true">
android:layout_width="164dp" android:layout_height="fill_parent"
android:paddingBottom="8dp" android:paddingLeft="8dp"
android:paddingTop="8dp" android:paddingRight="6dp" android:gravity="center"
android:background="#50000000" />
我的自定义LinearLayout
public class ARView extends LinearLayout
{
public ARView(final Context context, String name, String id)
{
super(context);
getLayoutInflater().inflate(R.layout.ar_item, this ,true);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.ar_item, null);
TextView textView = (TextView) findViewById(R.id.ar_item_txt);
textView.setText(name);
setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Toast t = Toast.makeText(context, "hey!", Toast.LENGTH_SHORT);
t.show();
}
});
}
}
15个解决方案
95 votes
android:duplicateParentState="true"没有帮助我。
要使布局及其子项可点击,您需要为每个子项添加此选项:
android:clickable="false"
然后,点击处理将转到父项。
Yurets answered 2019-11-08T17:33:53Z
57 votes
为每个孩子
android:duplicateParentState =“ true”
Blackbelt answered 2019-11-08T17:33:16Z
20 votes
这不是您的情况,但可点击的android:inputType存在类似的问题。在寻找解决方案一个小时后,发现我将android:inputType设置为TextView设置在我的ViewGroup内,这阻止了onClick()侦听器(不知道为什么)
不要在TextView中使用android:inputType
elementstyle answered 2019-11-08T17:34:26Z
2 votes
TextView的高度可以覆盖整个父级(整个布局),因此您可以单击空白区域,但不能单击布局。 尝试为您的TextView使用wrap_content用于android:layout_height。 还要为布局设置点击监听器。
Nikola Despotoski answered 2019-11-08T17:34:52Z
2 votes
您没有使用自定义视图; 您正在使用标准的LinearLayout。 您的XML标签应为:
...
cdhabecker answered 2019-11-08T17:35:17Z
2 votes
clickable="false"使我的TextView看起来像已被禁用,并且无法接收单击事件。
您只需要设置TextViewclickable="false"。因此click事件将分派到父布局,并且TextView仍然可以对touch事件做出反应(具有波纹效果)。
nhoxbypass answered 2019-11-08T17:35:50Z
2 votes
使您的父级LinearLayout的android:clickable =“ true”
使所有childview的android:clickable =“ false”
在Linearlayout下-从TextView中删除android:inputType =“”
Ahamadullah Saikat answered 2019-11-08T17:36:30Z
1 votes
要确保的一件事是,另一个视图不在您尝试单击的视图之上。 这在FrameLayouts(可能覆盖了您的子LinearLayout)中或在相对布局中尤其常见,您可能忘记更新此行:
android:layout_below="@id/shouldBeTheIdOfTheViewCurrentlyBeingCovered"
这样视图就不会占据相同的空间。
ColossalChris answered 2019-11-08T17:37:02Z
1 votes
如果有问题的视图是TextViews,则可能需要将它们设置为focusable="false"25,这样就不会将第一次单击集中在文本视图上。
CorayThan answered 2019-11-08T17:37:29Z
0 votes
问题可能出在布局中具有android:layout_height="fill_parent"的文本视图中。 如果那不能解决问题,则问题可能出在onClick()事件上。 由于线性布局是布局,因此实际上可能从未调用过onClick()。 尝试改写onTouch()事件侦听器。
A. Abiri answered 2019-11-08T17:37:55Z
0 votes
将以下属性添加到linearlayout子视图未处理的所有Click事件都将自动传递到LinearLayout。
...
android:focusable="true"
android:clickable="true">
Sanket Berde answered 2019-11-08T17:38:21Z
0 votes
我遇到了同样的问题,并且所有的XML属性都不起作用。 我不确定是否会发生这种情况,因为我以编程方式膨胀并添加了视图,但这就是我解决问题的方式。
我有一个扩展LinearLayout的类,其中包含TextView和ImageView。扩大布局并获取视图后,我为子视图分配了一个OnClickListener,当按下该按钮时,将执行LineaLayout的onClickListner。
public class MyView extends LinearLayout {
private OnClickListener clickListener;
ImageView imageView;
TextView textView;
@Override
public void setOnClickListener(@Nullable OnClickListener l) {
super.setOnClickListener(l);
clickListener = l;
}
void afterViews() {
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
clickListener.onClick(MyView.this);
return false;
}
});
textView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
clickListener.onClick(MyView.this);
return false;
}
});
}
我也尝试覆盖OnTouchListener,但是随后我的子视图没有起波纹效果,这是我所需要的。
Gilles Braun answered 2019-11-08T17:39:02Z
0 votes
上面的解决方案都不适合我。 相比之下,我注意到在LinearLayout内部设置了一个LinkMovementMethod设置为TextView。此类拦截了触摸事件,单击事件以及TextView的属性,例如可点击,可聚焦与可点击。
savepopulation answered 2019-11-08T17:39:30Z
0 votes
我遇到了同样的问题,并且所有XML属性均不起作用。 我认为发生这种情况是因为我以编程方式使视图膨胀并添加了视图。 对我来说,解决方法是-也是通过编程-将膨胀后的根视图设置为不可点击:
View view = layoutInflater.inflate(R.layout.some_layout, someLinearLayout, false);
view.setClickable(false);
(是的,我试图让some_layout在XML中不可点击。)
Roy Solberg answered 2019-11-08T17:40:03Z
0 votes
为LinearLayout创建变量
LinearLayout layout;
layout.setOnClicknersetOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Toast t = Toast.makeText(context, "hey!", Toast.LENGTH_SHORT);
t.show();
}
});
AZIM MOHAMAD answered 2019-11-08T17:40:31Z