我制作了两个微调器,它们都由不同类型的权重数组组成。这些是"磅"石"克"公斤"。我需要能够检查用户从这两个微调器中选择的选项,然后根据他们的选择调用一个函数。例如,如果第一个微调器等于"Lbs",第二个等于"Grams",那么就会调用一个函数。
这些是我的字符串
<string-array name="weightFSpinner">
<item>Lbs</item>
<item>Grams</item>
<item>Kilograms</item>
<item>Stone</item>
<item>Ounces</item>
</string-array>
<string-array name="weightSSpinner">
<item>Lbs</item>
<item>Grams</item>
<item>Kilograms</item>
<item>Stone</item>
<item>Ounces</item>
</string-array>
这是我的体重页面上的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weight_page);
// Code for first drop down list
final Spinner weightFSpinner = (Spinner) findViewById(R.id.weightFSpinner);
final ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(weightPage.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.weightFSpinner));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weightFSpinner.setAdapter(myAdapter);
// code for second drop down list
final Spinner weightSSpinner = (Spinner) findViewById(R.id.weightSSpinner);
final ArrayAdapter<String> myAdapter2 = new ArrayAdapter<String>(weightPage.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.weightSSpinner));
myAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weightSSpinner.setAdapter(myAdapter2);
Button convertBtn = (Button) findViewById(R.id.convertBtn);
convertBtn.setOnClickListener(new View.OnClickListener() {
//when the button is pressed this happens
@Override
public void onClick(View view) {
if(myAdapter.equals("Lbs") && myAdapter2.equals("Grams")){
poundsToGrams();
}
}
//converting pounds to grams
private void poundsToGrams() {
EditText convertTxt = (EditText) findViewById(R.id.convertTxt);
TextView resultTxt = (TextView) findViewById(R.id.resultTxt);
String firstWeight;
String secondWeight;
double pounds;
try {
pounds = parseDouble(convertTxt.getText().toString());
} catch
(NumberFormatException ex) {
convertTxt.setError("Must be int");
return;
}
double result = (pounds * 453.59237);
resultTxt.setText(result + " Grams");
}
// firstWeight = myAdapter.getItem(0);
//secondWeight = myAdapter2.getItem(1);
});
}
}
您可以使用以下方法:
1:如果不同的Spinner调整了相同的信息,则可以在它们之间共享适配器。一个OnItemSelectedListener将适用于这两个Spinner。您可以对作为参数传递的AdapterView调用getId((,以了解引发事件的Spinner。
@Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
switch(parentView.getId()){
case R.id.weightFSpinner:
fValue= weightFSpinner.getSelectedItem().toString();
break;
case R.id.weightSSpinner:
sValue= weightSSpinner.getSelectedItem().toString();
break;
case ...
}
}
2:将选定的值存储在2个变量中。项目是从微调器中选择的。
3:然后检查按钮的点击监听器中的变量值
将.oItemSelectedListener侦听器添加到两个微调器中,并覆盖其在NothingSelected和onItemSelected上的函数。
在两个微调器的onItemSelected中,将该位置的值保存为两个微调程序都可以访问的字符串。例如,将weightSSpinner的权重保存到字符串"myWeightS",将weight FSpinner的重量保存到字符串"myWeightF"。
然后在两个微调器的onItemSelected函数中,检查字符串是否相等。如果是,请执行您想要的功能。
if(myWeightS.equals(myWeightF))
{
//execute desired function
}
向微调器添加一个onItemSelectedListener
weightFSpinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
// here you can add the if clauses to check which spinner contains which value and call the method accordingly
}
public void onNothingSelected(AdapterView<?> parent) {
}
});