我试图在片段中设置一个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引起我注意,我没有为您提供解决方案,而是给您实现目标的另一种方法,所以我正在对此进行编辑。
首先:您的代码必须正常工作
您发布的代码是正确的
现在,进行一些检查:
最后执行以下操作:
之后,让我们知道
可能的解决方案:
您recycleview
在的顶部有个button
。您已将此视图的match parent
高度和宽度都设置在内relativelayout
,这意味着该视图将覆盖按钮。
删除此空白recycleview
,代码将起作用
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句