我认为我已经采取了所有必要的步骤来实现这一目标,但不知怎的,它仍然无法正常工作: - 片段的类实现了OnClickListener - 我从onClick事件中删除了
我在这里做错了什么?
首先,我将我的片段添加到我的活动中:
fragmentManager = getSupportFragmentManager();
if (savedInstanceState == null) {
HomeFragment homeFragment = new HomeFragment();
fragmentManager.beginTransaction()
.add(R.id.fragment_container, homeFragment)
.addToBackStack(null)
.commit();
我的片段XML看起来像这样:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="be.test.application.HomeFragment">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="be.prosteps.promoscanapp.HomeActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
/>
<include
android:id="@+id/drawer_layout"
layout="@layout/activity_drawer"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:id="@+id/imgLogo"
android:layout_below="@+id/toolbar"
android:background="@drawable/logo"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/txtWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_welcome"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:textStyle="bold"
android:layout_below="@+id/imgLogo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#000000" />
<TextView
android:id="@+id/txtWelcomeUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_user"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:textStyle="bold"
android:layout_below="@+id/txtWelcome"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="8dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#000000" />
<Button
android:id="@+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtWelcomeUser"
/>
</RelativeLayout>
我的片段代码是这样的:
public class HomeFragment extends Fragment implements View.OnClickListener {
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnTest:
Toast.makeText(getActivity(), "btnTest clicked", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getActivity(), "other button clicked", Toast.LENGTH_LONG).show();
break;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
final Button button = (Button) view.findViewById(R.id.btnTest);
button.setOnClickListener(HomeFragment.this);
return view;
}
}