0

我创建了一个 Spinner,并为我的应用程序填充了值,这将计算出饮料中的酒精单位。

旋转器容纳不同类型的饮料品脱、半品脱、瓶子和罐头。

酒精单位是通过将旋转器中的饮料量乘以饮料量乘以酒精百分比超过 1000 来计算的。

我想要发生的是,当用户从微调器中选择一些值(例如品脱)时,它会将一品脱的体积添加到方程中。

这是我的微调器代码

int pint,half,bottle,can;
Spinner beerspinner;
Spinner spinner = (Spinner) findViewById(R.id.beerspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.drinks_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

我想以某种方式将微调器中的值与整数匹配,以便我可以执行方程式。

pint = 568;
half = 284;
bottle = 275;
can = 440;

我还没有很好地解释它这里是我的完整代码,所以你可以更好地了解我想要做什么。

public class AppTestActivity extends Activity {
int pint,half,bottle,can;
Spinner beerspinner;
Button submit, reset;
EditText ABV,quantity;
TextView units, error;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ABV = (EditText) findViewById(R.id.ABV);
submit = (Button) findViewById(R.id.submit);
reset = (Button) findViewById(R.id.reset);
units = (TextView) findViewById(R.id.units);
error = (TextView) findViewById(R.id.error);
quantity= (EditText) findViewById(R.id.quantity);

Spinner spinner = (Spinner) findViewById(R.id.beerspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.drinks_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

submit.setOnClickListener(new clicker());
 }
class clicker implements Button.OnClickListener{

public void onClick(View v){

pint = 568;
half = 284;
bottle = 275;
can = 440;

String number,percentage;
Float calc; 

percentage = ABV.getText().toString();
number = quantity.getText().toString(); 
calc = Float.parseFloat(number)*Float.parseFloat(percentage)/1000;
units.setText(calc.toString());
}

目前,代码将饮料的数量乘以酒精百分比,然后除以 1000,我需要它来将微调器中的饮料类型添加到方程中。

4

1 回答 1

3

Crate an int in your activity.

int drinkType; 

Use setOnItemSelected listener for the spinner:

spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
      public void on ItemSlected(...)
          int selectedIndex=spinner.getSelectedItenPosition();

          if(selectedIndex==0) //pint for example
                drinkType = 568;
          //do this for other drink types
}

And in your code above you can do

calc = Float.parseFloat(number)*Float.parseFloat(percentage)/1000+ drinkType;
于 2012-04-21T19:51:40.790 回答