简体   繁体   English

使用微调器更改变量的值(Android Studio)

[英]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 这是来自Java的教程,可帮助您更好地理解它们。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 之后,您可以查看android developers studio的文档,他们还提供了很好的示例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 } }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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