gpt4 book ai didi

android - 带 fragment 的按钮

转载 作者:行者123 更新时间:2023-11-30 01:35:33 28 4
gpt4 key购买 nike

我正在为我在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com