对不起我的英语,但我是新手,并且我的代码有问题因为我想在我的片段中添加一个按钮去活动o另一个片段,我不知道如果这是可能的(片段 - >活动)我需要知道添加此按钮的方法是什么。
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
public void IniciarH(View v)
{
Intent intent=new Intent(getActivity(), IniciarHome.class);
startActivity(intent);
}
}
和我的.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">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="159dp"
android:onClick="IniciarH"
android:src="@drawable/ic_home" />
答案 0 :(得分:1)
将clickListener添加到Button
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
ImageButton imageButton = (ImageButton)rootView.findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(), IniciarHome.class);
startActivity(intent);
}
});
return rootView;
}