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 fragment实现按钮点击事件的示例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。