我正在使用切换按钮将单位转换的值转换为另一种。
意思是说,我将为计算输入所有必要的参数。从开关按钮中选择选项后,在我通过单击“计算”按钮开始计算后,将根据用户选择的单位输出。
然而,我面临的当前问题是即使没有点击“计算”按钮,我也可以使用开关按钮执行计算。
我知道我的问题应该在以下几行代码上。
double result = 0;
if (v == calButton) {
double lamda = 300000000 / freq;
result = tPower * tGain * rGain * Math.pow(lamda / (4 * Math.PI * distance), 2) / light / 100;
//double radius = 5000;
((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(Double.toString(result));
final double finalResult = result;
Switch select_unit=(Switch)llLayout.findViewById(R.id.switch1);
select_unit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(Double.toString(finalResult));
} else {
((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(Double.toString(10 * Math.log10(finalResult * 1000)));
}
}
});
从上面的代码来看,问题显然是因为我实现了“计算”和“计算”。 “切换”按钮的方法相同。我试图将OnCheckChanged方法从中分离出来。但是却遇到了错误。例如,它声明从不使用变量结果。
有人可以提供一些指南/提示吗?
这是我的界面:
我的完整代码:
package com.epsilon.rfcalculator.activity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;
import com.epsilon.rfcalculator.R;
public class FreeSpaceActivity extends Fragment implements OnClickListener {
EditText tPowerEdit = null;
EditText tGainEdit = null;
EditText rGainEdit = null;
EditText freqEdit = null;
EditText distanceEdit = null;
EditText lightEdit = null;
Button calButton = null;
Button graphButton = null;
Button clearButton = null;
Button plotButton = null;
Switch switchButton=null;
EditText radEdit = null;//testing
private ScrollView llLayout;
private FragmentActivity faActivity;
//private Switch switch1;
//@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
faActivity = (FragmentActivity) this.getActivity();
llLayout = (ScrollView) inflater.inflate(R.layout.activity_free_space, container, false);
tPowerEdit = (EditText)llLayout.findViewById(R.id.editText1);
tGainEdit = (EditText)llLayout.findViewById(R.id.editText2);
rGainEdit = (EditText)llLayout.findViewById(R.id.editText3);
freqEdit = (EditText)llLayout.findViewById(R.id.editText4);
distanceEdit = (EditText)llLayout.findViewById(R.id.editText7);
lightEdit = (EditText)llLayout.findViewById(R.id.editText8);
calButton = (Button)llLayout.findViewById(R.id.button1);
calButton.setOnClickListener(this);
graphButton = (Button)llLayout.findViewById(R.id.button2);
graphButton.setOnClickListener(this);
clearButton = (Button)llLayout.findViewById(R.id.button3);
clearButton.setOnClickListener(this);
plotButton =(Button)llLayout.findViewById(R.id.plot_button);
plotButton.setOnClickListener(this);
radEdit = (EditText)llLayout.findViewById(R.id.editText28);//testing
return llLayout;
}
/*private ActionBar getSupportActionBar() {
return null;
}*/
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tPowerStr = tPowerEdit.getText().toString();
String tGainStr = tGainEdit.getText().toString();
String rGainStr = rGainEdit.getText().toString();
String freqStr = freqEdit.getText().toString();
String distanceStr = distanceEdit.getText().toString();
String lightStr = lightEdit.getText().toString();
double tPower = Double.parseDouble(!tPowerStr.isEmpty() ? tPowerStr : "0");
double tGain = Double.parseDouble(!tGainStr.isEmpty() ? tGainStr : "0");
double rGain = Double.parseDouble(!rGainStr.isEmpty() ? rGainStr : "0");
double freq = Double.parseDouble(!freqStr.isEmpty() ? freqStr : "0");
double distance = Double.parseDouble(!distanceStr.isEmpty() ? distanceStr : "0");
double light = Double.parseDouble(!lightStr.isEmpty() ? lightStr : "0");
String radStr = radEdit.getText().toString();//testing
double radius = Double.parseDouble(!radStr.isEmpty() ? radStr : "0");//testing
double result = 0;
if (v == calButton) {
double lamda = 300000000 / freq;
result = tPower * tGain * rGain * Math.pow(lamda / (4 * Math.PI * distance), 2) / light / 100;
//double radius = 5000;
//((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(Double.toString(result));
final double finalResult = result;
Switch select_unit=(Switch)llLayout.findViewById(R.id.switch1);
select_unit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(Double.toString(finalResult));
} else {
((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(Double.toString(10 * Math.log10(finalResult * 1000)));
}
}
});
}
else if (v == graphButton) {
Intent intent = new Intent(getActivity(), GraphActivity.class);
intent.putExtra("model", "freeSpace");
intent.putExtra("tp", tPower);
intent.putExtra("tg", tGain);
intent.putExtra("rg", rGain);
intent.putExtra("f", freq);
intent.putExtra("l", light);
this.startActivity(intent);
} else if (v == clearButton) {
((EditText) llLayout.findViewById(R.id.editText1)).setText("");
((EditText) llLayout.findViewById(R.id.editText2)).setText("");
((EditText) llLayout.findViewById(R.id.editText3)).setText("");
((EditText) llLayout.findViewById(R.id.editText4)).setText("");
((EditText) llLayout.findViewById(R.id.editText7)).setText("");
((EditText) llLayout.findViewById(R.id.editText8)).setText("");
((EditText) llLayout.findViewById(R.id.editText28)).setText("");
((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText("");
//((TextView) llLayout.findViewById(R.id.textFreeSpacePrd)).setText("");
} else if (v == plotButton) {
//double radius = Double.parseDouble(radius.getText().toString());
Intent intent = new Intent(getActivity(), MapActivity.class);
Bundle b = new Bundle();
b.putDouble("radius", result);
intent.putExtras(b);
this.startActivity(intent);
}
}
}
答案 0 :(得分:0)
你已经在onClick(View v)方法中填充了所有内容。你确定吗?无论您在屏幕上的哪个位置,每次都会触发此事件。除非你需要,否则不要这样做。根据onCreateView方法中的要求,为每个控件单独执行此操作。您已在计算按钮的实现中实现了切换按钮的界面。将它们分开并将开关按钮的状态保存在某个全局变量中。访问计算按钮侦听器中的全局变量。希望能帮助到你!!一切顺利
答案 1 :(得分:0)
尝试更改代码:
public class FreeSpaceActivity extends Fragment implements OnClickListener {
EditText tPowerEdit = null;
EditText tGainEdit = null;
EditText rGainEdit = null;
EditText freqEdit = null;
EditText distanceEdit = null;
EditText lightEdit = null;
Button calButton = null;
Button graphButton = null;
Button clearButton = null;
Button plotButton = null;
Switch switchButton=null;
EditText radEdit = null;//testing
private ScrollView llLayout;
private FragmentActivity faActivity;
//private Switch switch1;
// Changes here
private double mResult = 0;
private Switch mSelectUnit;
private TextView mTextFreeSpaces;
private boolean mIsCalculated;// flag to show the result only when you click on calculate button
//@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
faActivity = (FragmentActivity) this.getActivity();
llLayout = (ScrollView) inflater.inflate(R.layout.activity_free_space, container, false);
tPowerEdit = (EditText)llLayout.findViewById(R.id.editText1);
tGainEdit = (EditText)llLayout.findViewById(R.id.editText2);
rGainEdit = (EditText)llLayout.findViewById(R.id.editText3);
freqEdit = (EditText)llLayout.findViewById(R.id.editText4);
distanceEdit = (EditText)llLayout.findViewById(R.id.editText7);
lightEdit = (EditText)llLayout.findViewById(R.id.editText8);
calButton = (Button)llLayout.findViewById(R.id.button1);
calButton.setOnClickListener(this);
graphButton = (Button)llLayout.findViewById(R.id.button2);
graphButton.setOnClickListener(this);
clearButton = (Button)llLayout.findViewById(R.id.button3);
clearButton.setOnClickListener(this);
plotButton =(Button)llLayout.findViewById(R.id.plot_button);
plotButton.setOnClickListener(this);
radEdit = (EditText)llLayout.findViewById(R.id.editText28);//testing
// Changes here
mTextFreeSpaces = (TextView) llLayout.findViewById(R.id.textFreeSpaces)
mSelectUnit =(Switch)llLayout.findViewById(R.id.switch1);
mSelectUnit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateFreeSpacesText(isChecked);
}
});
return llLayout;
}
/*private ActionBar getSupportActionBar() {
return null;
}*/
private void updateFreeSpacesText(boolean isSwitcherChecked) {
if(mIsCalculated){
if(isSwitcherChecked){
mTextFreeSpaces.setText(Double.toString(mResult));
} else {
mTextFreeSpaces.setText(Double.toString(10 * Math.log10(mResult* 1000)));
}
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tPowerStr = tPowerEdit.getText().toString();
String tGainStr = tGainEdit.getText().toString();
String rGainStr = rGainEdit.getText().toString();
String freqStr = freqEdit.getText().toString();
String distanceStr = distanceEdit.getText().toString();
String lightStr = lightEdit.getText().toString();
double tPower = Double.parseDouble(!tPowerStr.isEmpty() ? tPowerStr : "0");
double tGain = Double.parseDouble(!tGainStr.isEmpty() ? tGainStr : "0");
double rGain = Double.parseDouble(!rGainStr.isEmpty() ? rGainStr : "0");
double freq = Double.parseDouble(!freqStr.isEmpty() ? freqStr : "0");
double distance = Double.parseDouble(!distanceStr.isEmpty() ? distanceStr : "0");
double light = Double.parseDouble(!lightStr.isEmpty() ? lightStr : "0");
String radStr = radEdit.getText().toString();//testing
double radius = Double.parseDouble(!radStr.isEmpty() ? radStr : "0");//testing
if (v == calButton) {
double lamda = 300000000 / freq;
mResult = tPower * tGain * rGain * Math.pow(lamda / (4 * Math.PI * distance), 2) / light / 100;
mIsCalculated = true;
updateFreeSpacesText(mSelectUnit.isChecked());
}
else if (v == graphButton) {
Intent intent = new Intent(getActivity(), GraphActivity.class);
intent.putExtra("model", "freeSpace");
intent.putExtra("tp", tPower);
intent.putExtra("tg", tGain);
intent.putExtra("rg", rGain);
intent.putExtra("f", freq);
intent.putExtra("l", light);
this.startActivity(intent);
} else if (v == clearButton) {
((EditText) llLayout.findViewById(R.id.editText1)).setText("");
((EditText) llLayout.findViewById(R.id.editText2)).setText("");
((EditText) llLayout.findViewById(R.id.editText3)).setText("");
((EditText) llLayout.findViewById(R.id.editText4)).setText("");
((EditText) llLayout.findViewById(R.id.editText7)).setText("");
((EditText) llLayout.findViewById(R.id.editText8)).setText("");
((EditText) llLayout.findViewById(R.id.editText28)).setText("");
((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText("");
//((TextView) llLayout.findViewById(R.id.textFreeSpacePrd)).setText("");
} else if (v == plotButton) {
//double radius = Double.parseDouble(radius.getText().toString());
Intent intent = new Intent(getActivity(), MapActivity.class);
Bundle b = new Bundle();
b.putDouble("radius", result);
intent.putExtras(b);
this.startActivity(intent);
}
}
}