这是片段的代码。请帮我按一下按钮,onClick
它应该转到相应的活动
class boards extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.boards, container, false);
}
}
答案 0 :(得分:0)
内部onViewCreated
方法:
View view = inflater.inflate(R.layout.boards, container, false);
Button button = (Button) view.findViewById(R.id.buttonId);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
// do something
startActivity(new Intent(getActivity(), YourActivity.class));
}
});
return view;
答案 1 :(得分:0)
您需要实现回调接口
界面代码:
public interface FragmentCallback {
void changeActivity();
}
内部片段:
FragmentCallback mListener;
void setListener(FragmentCallback listener){
mListener=listener;
}
内部活动:
public class Activity extends AppCompatActivity implements FragmentCallback{
Boards fragment=new Boards(); //class name shoud be capital i.e Boards
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
fragment.setListener(this);
}
@Override
void changeActivity(){
Intent intent =new Intent(this,newActivity.class);
startActivity(intent);
}
}
使用以下代码更改片段中的活动:
if(mListener!=null)
mListener.changeActivity()
下面的代码将执行任务