我正在尝试制作一个使用微调器作为单位选择方法来转换距离/面积/体积的应用程序。计算意味着完成,然后根据输入到 EditText 中的内容将输出发送到 TextView 。但是输出只有 0.0,没有别的。有人可以帮忙吗?
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
switch(pos){
case 0:
option1.setAdapter(length);
option2.setAdapter(length);
return;
case 1:
option1.setAdapter(area);
option2.setAdapter(area);
return;
default:
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
});
option1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(pos>=0) {
stringInput = edittext1.getText().toString();
if(stringInput == null || stringInput.isEmpty()) {
doubleInput = 0.0;
}
else {
doubleInput = Double.parseDouble(edittext1.getText().toString());
}
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
});
option2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
switch(pos) {
case 0:
miles = doubleInput * 1;
textview1.setText("" + String.valueOf(miles));
return;
case 1:
km = doubleInput * 1.609344;
textview1.setText("" + String.valueOf(km));
return;
default:
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
});
最佳答案
冒着听起来像个老男人的风险,这看起来像是你的大学作业......是吗?
自从提出问题后,您的代码发生了变化,这使得很难回答!幸运的是,在您更改代码之前,我设法将您的代码抓取到 Eclipse 中......无论如何,在您的原始代码中,您在 create 方法中执行所有操作,此时您还没有为 edittext1 输入值(除非它被设置为一些合理的默认值,我认为它是 0,因此总是得到零作为你的答案?)
// Whilst setting up a view the create method will not have a
// reasonable value for edittext1 - or it will be your default
String stringInput = (edittext1.getText().toString());
if (stringInput.isEmpty()) {
doubleInput = 0.0; // Will always enter this line
} else {
doubleInput = Double.parseDouble(edittext1.getText().toString());
}
你复制了代码...
output1 = (TextView) findViewById(R.id.output1);
实际上 output1 到 output10(即所有十行)都是重复的。
至于您更新后的代码,它仍然给您带来问题吗?您确定 stringInput 有值吗?我的意思是你输入了什么吗?你可以通过调试你的程序来检查..
如 FloatingCoder 所建议的,以下内容也容易出错,并且可能会中断...
doubleInput = Double.parseDouble(edittext1.getText().toString());
一个更好的方法(因为它捕获了 Java 可能抛出的异常)是
doubleInput = 0.0;
String inputStr = question.getText().toString();
try {
doubleInput = Double.parseDouble(inputStr);
} catch (NumberFormatException e) {
// This should probably do something more useful? i.e. tell
// the user what they've done wrong...
Log.e("Android",
"Double throws a NumberFormatException if you enter text that is not a number");
}
哦,Android 有一些用于检查字符串的辅助实用程序,请参阅 TextUtils,或者只是我的示例...
if (!TextUtils.isEmpty(inputStr)) { // checks for "" and null (see documentation)
doubleInput = Double.parseDouble(inputStr);
}
我真的建议为看起来不正确的计算编写一个简单的测试用例,因为两个文本框和一个按钮真的不难放在一起,而且非常容易调试,而不需要所有微调器都进入方式...无论如何希望这有所帮助,哦,我的完整示例有两个编辑文本和一个按钮,我将在此处发布...希望它有所帮助...
private Button btnCalc;
private EditText question, answer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
answer = (EditText) findViewById(R.id.answer);
question = (EditText) findViewById(R.id.question);
btnCalc = (Button) findViewById(R.id.btnCalc);
// The OnClickListener here will be executed outside the "Create",
// i.e., when you actually click on the button, which will give you
// a chance to enter some values in the question edittext...
btnCalc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
double in = 0.0;
try {
String inputStr = question.getText().toString();
// if you want to check it use
if (!TextUtils.isEmpty(inputStr)) {
in = Double.parseDouble(inputStr);
}
} catch (NumberFormatException e) {
// This should probably do something more useful? i.e. tell
// the user what they've done wrong...
Log.e("Android",
"Double throws a NumberFormatException if you enter text that is not a number");
}
double miles = in * 1.6;
answer.setText(String.valueOf(miles));
}
});
}
关于java - 为什么我不能让这些计算显示在 TextView 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8731247/