当前位置:主页 > 移动开发 > Android代码 >

Android fragment实现按钮点击事件的示例讲解

时间:2021-09-11 08:09:13 | 栏目: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));
   }
  });
 }

您可能感兴趣的文章:

相关文章