您好我在整个单个活动中传输变量值时遇到问题,根据if语句和变量值进行计算。
private EditText edittext1;
private TextView option1, option2, option3, option4, option5,
option6, option7, option8, option9, option10,
output1, output2, output3, output4, output5,
output6, output7, output8, output9, output10,
textview16;
private int unitGroup, calculate;
String meter, squareMeter, centimeter;
double doubleInput, out1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.converter);
ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
Spinner spinner = (Spinner) findViewById(R.id.converter);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.convert_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
final ArrayAdapter<CharSequence> length = ArrayAdapter.createFromResource(
this, R.array.length_array, android.R.layout.simple_spinner_item);
length.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter<CharSequence> area = ArrayAdapter.createFromResource(
this, R.array.area_array, android.R.layout.simple_spinner_item);
area.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
edittext1 = (EditText)findViewById(R.id.editText1);
textview16 = (TextView)findViewById(R.id.TextView16);
// Get unit option textview's
option1 = (TextView)findViewById(R.id.option1);
option2 = (TextView)findViewById(R.id.option2);
option3 = (TextView)findViewById(R.id.option3);
option4 = (TextView)findViewById(R.id.option4);
option5 = (TextView)findViewById(R.id.option5);
option6 = (TextView)findViewById(R.id.option6);
option7 = (TextView)findViewById(R.id.option7);
option8 = (TextView)findViewById(R.id.option8);
option9 = (TextView)findViewById(R.id.option9);
option10 = (TextView)findViewById(R.id.option10);
// Get unit output textview's
output1 = (TextView)findViewById(R.id.output1);
output2 = (TextView)findViewById(R.id.output2);
output3 = (TextView)findViewById(R.id.output3);
output4 = (TextView)findViewById(R.id.output4);
output5 = (TextView)findViewById(R.id.output5);
output6 = (TextView)findViewById(R.id.output6);
output7 = (TextView)findViewById(R.id.output7);
output8 = (TextView)findViewById(R.id.output8);
output9 = (TextView)findViewById(R.id.output9);
output10 = (TextView)findViewById(R.id.output10);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(pos==0) {
spinner1.setAdapter(length);
option1.setText("Meter");
option2.setText("Centimeter");
option3.setText("Millimeter");
option4.setText("Kilometer");
option5.setText("Inch");
option6.setText("Foot");
option7.setText("Yard");
option8.setText("Mile");
option9.setText("Astronomical Unit (AU)");
option10.setText("Lightyear");
unitGroup = 1;
}
if(pos==1) {
spinner1.setAdapter(area);
option1.setText("Square Meter");
option2.setText("Square Centimeter");
option3.setText("Square Millimeter");
option4.setText("Square Kilometer");
option5.setText("Square Inch");
option6.setText("Square Foot");
option7.setText("Square Yard");
option8.setText("Square Mile");
option9.setText("Acre");
option10.setText("Hectare");
unitGroup = 2;
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
});
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
switch(pos) {
case 0:
if (unitGroup == 1) {
calculate = 0;
}
if (unitGroup == 2) {
calculate = 0;
}
return;
case 1:
if (unitGroup == 1) {
calculate = 1;
}
if (unitGroup == 2) {
calculate = 1;
}
return;
default:
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
});
String stringInput = (edittext1.getText().toString());
if(stringInput.isEmpty()) {
doubleInput = 0.0;
}
else {
doubleInput = Double.parseDouble(edittext1.getText().toString());
}
textview16.setText("" + unitGroup);
if (calculate == 0) {
out1 = doubleInput * 1;
}
output1.setText("" + out1);
if (calculate == 1) {
out1 = doubleInput * 100;
}
output2.setText("" + out1);
}
无论我尝试什么,只显示为0或0.0。是否与未传输的变量信息有关?
答案 0 :(得分:0)
看起来你从未在听众中读过输入。您创建视图,然后创建听众,然后在edittext1
时阅读onCreate
中的文字。如果您想在用户有机会与其进行交互后阅读该字段,则需要在侦听器中读取edittext1
中的值。