简体   繁体   中英

Changing the value of a variable using a spinner (Android Studio)

I've been searching for awhile but I can't seem to find a solution to my problem. I'm still pretty new to programming. I'm trying to understand how to change the value of a single variable using a spinner. Ideally the user should select "Male" or "Female" from the spinner and receive different outputs based on their selection.

   //Two
   float Male=(float) (weight*1.9);
   float Female=(float) (weight*1.5)`

you will have to work with listeners

Here is the tutorial from java which will help you to understand them better https://docs.oracle.com/javase/tutorial/uiswing/events/intro.html

after that you can look into the documentation from android developers studio they also give good examples https://developer.android.com/guide/topics/ui/controls/spinner.html

You can bind your spinner with on selected listener.

spinnerObj.setOnItemSelectedListener(new OnItemSelectedListener() { 
@Override public void onItemSelected(AdapterView<?> adapter, View v,int position, long id) 
{ 
if(position == 0)
//Male 
else
//Female
}

 @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM