我在界面上显示输出时遇到了麻烦。
我的程序应该能够在输入一些参数时显示相应的输出。如果没有输入参数,则应显示“无结果”,这是我实施的。
但是,即使输入了参数。根据给定的公式,它仍然显示“无结果”而不是答案。
我的代码如下:
public class FreeSpaceActivity extends Fragment implements OnClickListener {
DecimalFormat disp_W = new DecimalFormat("0.000E0");
DecimalFormat disp_dBm = new DecimalFormat("0.000");
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;
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
private boolean flag;
//@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(null);
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 && flag==true){
if(isSwitcherChecked){
double dBm=10 * Math.log10(mResult* 1000);
mTextFreeSpaces.setText(disp_dBm.format(dBm));//display dbm
Toast.makeText(getActivity().getApplicationContext(),"Unit Chosen is dBm", Toast.LENGTH_LONG).show();
} else {
double W=mResult;
mTextFreeSpaces.setText(disp_W.format(W));//display W
Toast.makeText(getActivity().getApplicationContext(),"Unit Chosen is W", Toast.LENGTH_LONG).show();
}
}
}
@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 : "1");
double light = Double.parseDouble(!lightStr.isEmpty() ? lightStr : "1");
String radStr = radEdit.getText().toString();//testing
double radius = Double.parseDouble(!radStr.isEmpty() ? radStr : "0");//testing
if (v == calButton) {
if(mResult!=0) {
double lamda = 300000000 / freq;
mResult = tPower * tGain * rGain * Math.pow(lamda / (4 * Math.PI * distance), 2) / light / 100;
mIsCalculated = true;
flag = true;
updateFreeSpacesText(mSelectUnit.isChecked());
}
else
mTextFreeSpaces.setText("No results");
}
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) {
flag=false;
double lamda = 300000000 / freq;
((EditText) llLayout.findViewById(R.id.editText1)).setText(null);
((EditText) llLayout.findViewById(R.id.editText2)).setText(null);
((EditText) llLayout.findViewById(R.id.editText3)).setText(null);
((EditText) llLayout.findViewById(R.id.editText4)).setText(null);
((EditText) llLayout.findViewById(R.id.editText7)).setText(null);
((EditText) llLayout.findViewById(R.id.editText8)).setText(null);
((EditText) llLayout.findViewById(R.id.editText28)).setText(null);
((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(null);
//((TextView) llLayout.findViewById(R.id.textFreeSpacePrd)).setText("");
//mIsCalculated = true;
//updateFreeSpacesText(mSelectUnit.isChecked());
//mResult=tPower * tGain * rGain * Math.pow(lamda / (4 * Math.PI * distance), 2) / light / 100;
//mTextFreeSpaces.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)
我认为Stultuske是正确的,但我的声望很低,无法评论。
我希望你改变IF-Clause的陈述
if (v == calButton) {
if(mResult!=0) {
.
.
.
}
else
mTextFreeSpaces.setText("No results");
除此之外,您每次都会在else分支中运行,因为您的变量mResult仍为0并且没有更改。
如果Edittexts不像
那样是空的,也许你可以撤退 if(tPowerStr.matches("") && The other Edittexts) {
.
.
.
}
else
mTextFreeSpaces.setText("No results");
编辑: 顺便说一句,我对您的应用程序感兴趣,因为我是定向无线电和卫星通信工程师。