Sar*_*nce 5 java android android-studio
我试图在我的片段中设置一个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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的布局:
<?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>
Run Code Online (Sandbox Code Playgroud)
有没有人知道为什么当我按下按钮时onClick方法永远不会被调用?我错过了什么吗?
由于塞文让我注意到我没有给你一个解决方案,而是一个实现目标的替代方法,所以我正在编辑这个。
第一:你的代码必须有效
您发布的代码是正确的
现在,做一些检查:
最后进行以下一些操作:
之后,请告诉我们
可能的解决方案:
你有一个recycleview
位于button
. 您在 a 内设置了此视图的match parent
高度和宽度relativelayout
,这意味着此视图将覆盖按钮。
删除这个空recycleview
,代码就可以工作了
归档时间: |
|
查看次数: |
143 次 |
最近记录: |