android - 带 fragment 的按钮

标签 android android-fragments button onclick onclicklistener

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

fragment 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);
    }
}

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

谢谢!

最佳答案

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

替换

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

return view;

onCreateView()方法中

关于android - 带 fragment 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35151803/

相关文章:

c# - 如何根据按钮点击设置Label的内容

android - NDK-构建错误

android - map 框安卓 : Zoom over specific route

android - 可折叠 View 不适用于 viewpager

android - 如何从 Glide 获取图像的宽度和高度

android - 如何使用 FragmentPagerAdapter 在 fragment 之间滑动

html - 将 bootstrap-4 按钮向右移动而不会弄乱格式

HTML 和 CSS。社交按钮如何移动它们?

android - 如何更新不在 google play、i store 或 windows store 上的应用程序

android - Android Wear 是否支持 HttpURLConnection - 获取 EOFException