如何在onCreateView()中单击按钮功能?

时间:2018-04-26 15:15:49

标签: android button fragment-oncreateview

我在onCreateView方法中有这段代码,

  

代码

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_one, container, false);
    }
  

这是按钮单击功能。我需要将其添加到onCreateView Method.

            btnCheckout = (Button) view.findViewById(R.id.btnCheckout);
            btnCheckout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getContext(), SecondActivity.class);
                    startActivity(intent);
                }
            });
  

此行的错误为Unreachable Statement

btnCheckout = (Button) view.findViewById(R.id.btnCheckout);

如何添加该代码。有谁能够帮我?提前谢谢。

1 个答案:

答案 0 :(得分:0)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_one, container, false);

    btnCheckout = (Button) view.findViewById(R.id.btnCheckout);

    btnCheckout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    return view;
}