本文将带你了解Android应用开发Android fragment实现按钮点击事件的示例讲解,希望本文对大家学Android有所帮助。
fragment无法直接进行点击事件,需要放到oncreatActivity中
代码如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, null);
return view;
}
点击事件代码:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button sweepButton = (Button) getActivity().findViewById(R.id.image1);
sweepButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "nihao", Toast.LENGTH_LONG).show();
//从fragment跳转到activity中
startActivity(new Intent(getActivity(), PayMoneyActivity.class));
}
});
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!