用浮动动作按钮从碎片开始新活动
我目前正在尝试通过单击名为 FoodJournalFragment 的片段(我的 MainActivity.java 活动的片段)中的浮动操作按钮来启动名为 FoodJournalEntry 的新活动。但是,虽然我没有收到任何错误,但我的 setOnClickListener 函数没有执行任何操作。浮动操作按钮的 id 是“fab”。请帮忙!
这是我现在的代码。我试图让 Toast 东西显示某种消息,以便我可以判断按钮是否正常工作,但实际上什么也没有发生。我确实看到 logcat 识别出我认为的点击,但仅此而已。
我在片段的 onCreateView 内部编写的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.homepage, container, false);
FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
fab.setOnClickListener(view -> {
Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), FoodJournalEntry.class);
startActivity(intent);
});
return v;
}
片段的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id ="@+id/tasksText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Tasks"
android:textColor="@android:color/black"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:textSize="32sp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tasksRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tasksText"
android:nestedScrollingEnabled="true"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="32dp"
android:clickable="true"
android:backgroundTint="@android:color/holo_blue_dark"
android:src="@drawable/ic_baseline_add" />
</RelativeLayout>
I am currently trying to start a new activity called FoodJournalEntry from the click of a floating action button in a fragment called FoodJournalFragment (a fragment of my MainActivity.java activity). However, although I am getting no errors, my setOnClickListener function isn't doing anything. The id of the floating action button is "fab". Please help!
here is the code i have right now. I am trying to get the Toast thing to display some sort of message so I can tell if the button is even working, but literally nothing is happening. I do see the logcat recognizing the click i think but that's it.
The code I am writing inside of the onCreateView in the fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.homepage, container, false);
FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
fab.setOnClickListener(view -> {
Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), FoodJournalEntry.class);
startActivity(intent);
});
return v;
}
XML file for fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id ="@+id/tasksText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Tasks"
android:textColor="@android:color/black"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:textSize="32sp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tasksRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tasksText"
android:nestedScrollingEnabled="true"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="32dp"
android:clickable="true"
android:backgroundTint="@android:color/holo_blue_dark"
android:src="@drawable/ic_baseline_add" />
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论