带碎片的按钮

Buttons with fragments

我正在 android 工作室为我的学校项目开发一个应用程序,该应用程序与导航抽屉和多个片段一起使用,我已经设置并运行了所有片段,但现在我被卡住了。我不知道如何在片段中制作工作按钮。

碎片activity

    public class CaloriesEaten extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static EditText caleaten;
    private static EditText calalready;
    private static TextView caltotal;
    private static Button btnadd;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public CaloriesEaten() {
        // Required empty public constructor
    }

    public static CaloriesEaten newInstance(String param1, String param2) {
        CaloriesEaten fragment = new CaloriesEaten();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {        View view =  inflater.inflate(R.layout.fragment_calories_eaten,
            container, false);


        caleaten = (EditText) view.findViewById(R.id.CalorieInput);
        calalready = (EditText) view.findViewById(R.id.Cals);
        caltotal = (TextView) view.findViewById(R.id.CalNumber);
        btnadd = (Button) view.findViewById(R.id.addcalories);
        // Inflate the layout for this fragment
        btnadd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonClicked(v);
            }
        });
        return inflater.inflate(R.layout.fragment_calories_eaten, container, false);
    }
    public void buttonClicked (View view) {
        int x = Integer.parseInt(caleaten.getText().toString());

        int y = Integer.parseInt(calalready.getText().toString());

        int total = x + y;

        caltotal.setText(Integer.toString(total));
    }
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

这是我第一次在这里提问,所以如果我遗漏了什么,请告诉我。

谢谢!

有趣的原因: 你正在膨胀一个 View,用它来得到它 children 最后 return 不同的膨胀视图实例.您应该 return 与您最初膨胀并用于获取它的相同实例 children。

改变一下

return inflater.inflate(R.layout.fragment_calories_eaten, container, false);

return view;

onCreateView()方法中

通过在 onCreateView() 方法结束时膨胀来覆盖视图

替换

return inflater.inflate(R.layout.fragment_calories_eaten, container, false);

return view;

onCreateView()方法中

替换

return inflater.inflate(R.layout.fragment_calories_eaten, container, false);

view = inflater.inflate(R.layout.fragment_calories_eaten, container, false);
return view;

现在使用这个视图实例,您可以找到其他视图作为按钮在其上膨胀。

btnClickMe = (Button)view.findViewById(R.id.btn);

在返回视图之前在 onCreateView 方法中使用上面的代码。