我正在尝试转换来自EditText
getInput的输入并将输出设置为EditText
setOutput,但setOutput未显示结果的单位。我不明白为什么。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText getInput = (EditText) findViewById(R.id.input);
final EditText setOutput = (EditText) findViewById(R.id.output);
Button convert = (Button) findViewById(R.id.button);
double factor;
final String units [] = {
"Centimeter",
"Metres",
"Kilometres",
"Feet"
};
final Spinner unitsSpinner = (Spinner) findViewById(R.id.out);
final Spinner unitsSpinner1 = (Spinner) findViewById(R.id.in);
ArrayAdapter<String> A = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,
units);
unitsSpinner.setAdapter(A);
unitsSpinner1.setAdapter(A);
convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = unitsSpinner.getSelectedItemPosition();
int position2 = unitsSpinner1.getSelectedItemPosition();
double inputValue = Double.valueOf(getInput.getText().toString());
double outputValue;
if (position == 0 && position2 == 1){
outputValue = inputValue * 0.01;
String output = Double.toString(outputValue);
setOutput.setText(output);
}
if (position == 0 && position2 == 2){
outputValue = inputValue * 0.001;
String output = Double.toString(outputValue);
setOutput.setText(output);
}
if (position == 0 && position2 == 3){
outputValue = inputValue * 0.032;
String output = Double.toString(outputValue);
setOutput.setText(output);
}
}
});
}
答案 0 :(得分:0)
试用此代码
convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = unitsSpinner.getSelectedItemPosition();
int position2 = unitsSpinner1.getSelectedItemPosition();
double outputValue;
double inputValue = Double.parseDouble(getInput.getText().toString());
if (position == 0 && position2 == 1){
outputValue = inputValue * 0.01;
}
if (position == 0 && position2 == 2){
outputValue = inputValue * 0.001;
}
if (position == 0 && position2 == 3){
outputValue = inputValue * 0.032;
}
String output = Double.toString(outputValue);
setOutput.setText(output);
}
});