我有一个带有2个项目的微调器,但是当我选择其中一个项目时,我想使用不同的计算。
如何从另一种方法访问我的微调器项目中的一个?
在我的琴弦中看起来像这样;
<string-array name="inputGender">
<item> Kilo en centimeter</item>
<item> Pounds and inches</item>
</string-array>
我想要做的是对我的两个项目使用2种不同的计算方法。计算可以在我的calcBMI方法中找到。
我的活动现在看起来像这样。
public class OpenBMI extends Activity {
private EditText heightIn;
private EditText weightIn;
private TextView bmiOut;
private TextView bmiStatus;
private Button calcBMI;
private double weight = 0;
private double height = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_open_bmi);
initializeApp();
//Spinner 2
Spinner spinGender;
//Gets spinner from layout
spinGender = (Spinner) findViewById(R.id.spinGender);
//Gets string for spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources()
.getStringArray(R.array.inputGender));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinGender.setAdapter(adapter);
}
public void initializeApp() {
// TODO Auto-generated method stub
weightIn = (EditText) findViewById(R.id.inputWeight);
heightIn = (EditText) findViewById(R.id.inputLength);
calcBMI = (Button) findViewById(R.id.calcBMI);
bmiOut = (TextView) findViewById(R.id.bmiOut);
bmiStatus = (TextView) findViewById(R.id.bmiStatus);
calcBMI.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
calcBMI();
}
});
}
public void calcBMI()
{
String status;
weight = Double.parseDouble( weightIn.getText().toString() );
height = Double.parseDouble( heightIn.getText().toString() );
double bmi = ( weight/(height * height));
// double bmi2 = ( weight/(height * height) * 703);
String result = String.format("%.2f", bmi);
Log.d("MyActivity", result);
bmiOut.setText(result, TextView.BufferType.NORMAL );
if (bmi < 16.0){
status = "Seriously Underweight";
}else if ( bmi >= 16.0 && bmi < 18.0)
{
status = "Underweight";
}else if ( bmi >=18.0 && bmi < 24.0){
status = "normal";
}else { status = "Overweight" ;
}
bmiStatus.setText(status);
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
将微调器项目设为
String genderValue = spinGender.getSelectedItem().toString();
答案 2 :(得分:0)
实施OnItemSelectedListener 然后这两种方法......
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}