【问题标题】:My Android Button needs to be tapped twice to work我的 Android Button 需要点按两次才能工作
【发布时间】:2018-07-11 14:21:09
【问题描述】:

我最近遇到一个问题,我的 android 按钮需要点击两次才能工作。我已经做了很多研究,但这些解决方案都没有帮助。这是我的代码:

这是我定义按钮的方式:

<Button
    android:id="@+id/add_to_list_button"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:focusableInTouchMode="false"
    android:text=""
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

它是Recycler View Cell 中的一个按钮。以下是我在 Adapter 中设置 OnclickListener 方法的方法! - 不是活动或片段:(我已经在适配器中定义了这个按钮)

mAddButton = (Button) itemView.findViewById(R.id.add_to_list_button);

这里是onBindViewHolder 方法

holder.mAddButton.setBackgroundResource(R.mipmap.ic_add_hs_24dp);
holder.mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https:xxx"));
                v.getContext().startActivity(browserIntent);
            }
        });

希望你们能帮我找出问题所在。我已经为此工作了两天。非常感谢您的帮助。

【问题讨论】:

  • 父(列表项)布局是否也可选择?
  • 我看到你使用android:focusableInTouchMode="false",为什么?是否有任何其他视图将此属性设置为 true?
  • @mTak,不,我设置它的原因是因为这是 StackOverflow 的解决方案之一。如果我删除此属性,它仍然不起作用
  • 在布局中搜索具有相似属性的其他视图,这些视图可能会在第一次触摸时获得焦点

标签: java android android-button


【解决方案1】:

如果您的 RecyclerView 包含接受输入事件的组件,例如 EditText 或 Spinner,那么在 OnItemClickListener 或 OnItemLongClickListener() 中捕获点击事件可能会出现问题。

可能的解决方案是将您的 EditText/Spinner 设置为禁用事件,以便将点击事件降级到按钮

你可以通过设置来做到这一点

android:focusable="false"

android:focusableInTouchMode="false"

【讨论】:

  • 我没有 EditText 或 Spinner,还有其他想法吗?
猜你喜欢
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 2020-12-07
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多