我知道这可能很简单,但我的应用程序无效。我需要在Edittext
中输入权重并使用其值,方法CalculateDistance
的总距离值和CalculateCalories
方法的值。
使用此代码,我无法点击Start
按钮。
我在哪里必须添加从Edittext
获取值的代码以及如何使其工作?
我的代码目前看起来像这样:
public class RunningModeFragment extends Fragment implements IGPSActivity
{
MyLocation myLocation=null;
Button btnRunningModeStart=null;
EditText txtWeight=null;
TextView txtTotalDistance=null;
TextView txtCalories=null;
Button btnShowRunningHistory = null;
String weight;
Float weightInKg;
Float caloriesBurned;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.running_mode_fragment, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
txtTotalDistance=(TextView) getView().findViewById(R.id.txtTotalDistance);
txtWeight=(EditText) getView().findViewById(R.id.txtWeight);
txtCalories=(TextView) getView().findViewById(R.id.txtCalories);
if (txtWeight.getText().toString().isEmpty())
{
return;
}
else {
weight = txtWeight.getText().toString();
weightInKg = Float.parseFloat(weight);
}
btnRunningModeStart = (Button) getView().findViewById(R.id.btnRunningModeStart);
btnRunningModeStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
onClick_StartRunningMode(v);
}
});
btnShowRunningHistory = (Button) getView().findViewById(R.id.btnShowRunningHistory);
btnShowRunningHistory.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClick_ShowRunningHistory(v);
}
});
}
private void onClick_ShowRunningHistory(View v) {
Fragment fragment = new RunningHistoryFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment, "Running History");
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}
private void onClick_StartRunningMode(View v)
{
if(myLocation ==null)
{
new Thread(new Runnable() {
public void run() {
DatabaseFacade dbfacade = new DatabaseFacade(getView().getContext());
Activity activity = new Activity(ActivityMode.RUNNING);
activity.setActivityId(activity.getActivityId());
activity.setStart(new Timestamp(new Date().getTime()));
dbfacade.saveActivity(activity);
}
}).start();
Toast.makeText(this.getActivity(), "Start Running mode", Toast.LENGTH_SHORT).show();
myLocation = new MyLocation();
myLocation.LocationStart(this);
btnRunningModeStart.setText("Stop");
}
else
{
Toast.makeText(this.getActivity(), "Stop Running mode", Toast.LENGTH_SHORT).show();
myLocation.LocationListenerStop();
myLocation = null;
btnRunningModeStart.setText("Start");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
Location lastPoint = null;
float totalDistance = 0;
public static float CalculateDistance(Location startPoint, Location endPoint)
{
return (startPoint.distanceTo(endPoint) / ((float)1000));
}
public static float CalculateCalories(float a, float b){
return ((float)1.036)*a*b;
}
@Override
public void locationChanged(Location location)
{
try
{
if(lastPoint==null)lastPoint = location;
totalDistance += CalculateDistance(lastPoint, location);
lastPoint=location;
txtTotalDistance.setText(totalDistance + " km");
caloriesBurned=CalculateCalories(totalDistance,weightInKg);
txtCalories.setText(caloriesBurned + " kcal");
DatabaseFacade dbf = new DatabaseFacade(getView().getContext());
Activity activity = new Activity(ActivityMode.RUNNING);
dbf.saveLocation(new GpsLocation(activity.getActivityId(), location.getLongitude(), location.getLatitude(), location.getAccuracy()));
}catch (Exception E)
{
E.printStackTrace();
}
}
public void onResume(){
super.onResume();
((Main) getActivity()).setActionBarTitle("Running mode");
NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view);
// navigationView.setNavigationItemSelectedListener((Main) getActivity());
navigationView.setCheckedItem(R.id.runningMode);
}
}
我需要做以下事情: 从EditText txtWeight获取值,将其解析为float并在CalculateCalories方法中使用
public static float CalculateCalories(float a, float b){
return ((float)1.036)*a*b;
}
我试过这个,但它没有用
if (txtWeight.getText().toString().isEmpty())
{
return;
}
else {
weight = txtWeight.getText().toString();
weightInKg = Float.parseFloat(weight);
}
答案 0 :(得分:0)
首先创建一个视图而不是在onCreateView中初始化textview,就像这样,
View view = inflater.inflate(R.layout.fg_unapproved_leave, container, false);
Textview tv= view.findViewById(R.id.tv);
你在onViewCreated()中编写的代码应该在onCreateVIew()中。从onViewCreated()中删除它。
答案 1 :(得分:0)
你可以把它放在onCreateView()
上@jitendra purohit告诉你,如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fg_unapproved_leave, container, false);
txtTotalDistance=(TextView) view.findViewById(R.id.txtTotalDistance);
txtWeight=(EditText) view.findViewById(R.id.txtWeight);
txtCalories=(TextView) view.findViewById(R.id.txtCalories);
...
return view;
}
或者您可以将代码保存在onViewCreated()
中,而不是getView()
,您可以执行getActivity()
这样的代码:
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
txtTotalDistance=(TextView) getActivity().findViewById(R.id.txtTotalDistance);
txtWeight=(EditText) getActivity().findViewById(R.id.txtWeight);
txtCalories=(TextView) getActivity().findViewById(R.id.txtCalories);
...
请参阅此question/answers以及......
您可以在onClick()
中执行此操作,或者如果您希望动态显示更改,请使用TextWatcher
。
使用TextWatchers
TextWatcher textWatcher = new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (txtWeight.getText().toString().isEmpty()) {
return;
}
else {
weight = txtWeight.getText().toString();
weightInKg = Float.parseFloat(weight);
//Call your CalculateCalories method
}
}
};
}
使用onClick
我不会实现它,但您可以使用Button
或同一EditText
来执行此操作,并执行与afterChangedText()
相同的操作{1}}上的{1}}。
请记住TextWatcher
,你必须这样做:
onCreateView()
答案 2 :(得分:0)
放那行==> weight = txtWeight.getText()。toString();在onCLick方法
btnShowRunningHistory = (Button) getView().findViewById(R.id.btnShowRunningHistory);
btnShowRunningHistory.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (txtWeight.getText().toString().isEmpty())
{
return;
}
else {
weight = txtWeight.getText().toString();
weightInKg = Float.parseFloat(weight);
}
onClick_ShowRunningHistory(v);
}
});