片段中的onClick方法永远不会被调用

萨拉·林斯(Sara Lince)

我试图在片段中设置一个onClickListener。

public class HomeFragment extends Fragment implements View.OnClickListener {

Button btn_eventList;
public HomeFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    btn_eventList = (Button) view.findViewById(R.id.buttonEventList);
    btn_eventList.setOnClickListener(this);

    return view;
}

@Override
public void onClick(View v) {
    btn_eventList.setText("TEST");
    Toast.makeText(getActivity(), "TEST" , Toast.LENGTH_SHORT).show();
}

}

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nvd"
    android:layout_height="match_parent"
    android:layout_width="match_parent">



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:fontFamily="sans-serif"
                    android:gravity="center"
                    android:text="test"
                    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
                    />

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/textView2"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="1dp"
                    android:gravity="center"
                    android:text="test"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginTop="45dp"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/buttonEventList"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />
            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </android.support.v7.widget.RecyclerView>

        </LinearLayout>

    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/lst_nav_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        >

    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>

有谁知道为什么当我按下按钮时就永远不会调用onClick方法?我想念什么吗?

皮埃尔·乔治·米斯利

编辑

由于Sevin引起我注意,我没有为您提供解决方案,而是给您实现目标的另一种方法,所以我正在对此进行编辑。

首先:您的代码必须正常工作

您发布的代码是正确的

现在,进行一些检查:

  • 检查在调试模式下按钮是否不为null(应该抛出异常)
  • 检查在调试模式下您是否到达了onClick,即使文本没有更改,也没有出现吐司。
  • 检查是否正确显示了片段。这是有关如何创建片段的官方文档。
  • 检查片段是否可单击以及其上是否有任何内容(简单地,一旦单击按钮,就会显示经典的“单击”动画?)
  • 在xaml代码中签入您使用的ID是否已分配给正确的按钮。

最后执行以下操作:

  • 清理您的项目
  • 重建项目
  • 检查在xaml和Java代码中是否都具有警报(在基本配置中,该行上的黄色标记)
  • 卸载该应用程序并重新安装(大致相同,但是由于我们不知道您的项目,因此仍然可以提供帮助)

之后,让我们知道

可能的解决方案:

recycleview在的顶部有个button您已将此视图的match parent高度和宽度都设置在内relativelayout,这意味着该视图将覆盖按钮。

删除此空白recycleview,代码将起作用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章