简体   繁体   English

在Android开发中使用Spinner

[英]using spinner in android development

I'm new to android development, I have some experiences on C# windows programming, but also very little. 我是android开发的新手,我对C#Windows编程有一些经验,但也很少。 I'm working on an android application which uses spinners. 我正在使用使用微调器的android应用程序。 So far, I found a tutorial which make spinner items, and displays spinner text in text view, as you select spinner item. 到目前为止,我找到了一个制作微调器项目的教程,并在您选择微调器项目时在文本视图中显示微调器文本。 The code lookes like this: 代码看起来像这样:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    spinner =(Spinner)findViewById(R.id.sp);
    text=(TextView)findViewById(R.id.tv);
    ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this, R.array.spinnerarray, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner.setAdapter(adapter);
  spinner.setOnItemSelectedListener(new function());
}


public class function implements OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
            long id) {
        // TODO Auto-generated method stub
        String str=parent.getItemAtPosition(pos).toString();
        text.setText(str);

    }

I want to make an application, which will calculate some points, with the grade user would choose. 我想制作一个应用程序,它将根据用户选择的年级来计算一些分数。 Example: spinner will contain grades, such as Negative(1), Positive(2),Good(3), Very Good(4), Excellent(5). 示例:微调框将包含分数,例如负(1),正(2),好(3),非常好(4),优秀(5)。 I want to achieve, that when an user chooses one grade, lets say very good, that the application will use the grade for very good which is 4, and it would transform it to some points, determined by me ( 1=20, 2=40,3=60,4=80,5=100). 我想要实现的是,当用户选择一个等级时,可以说非常好,该应用程序将非常好地使用该等级,即4,并将其转换为我确定的一些点(1 = 20,2 = 40,3 = 60,4 = 80,5 = 100)。 The points will be later used in calculation, but I have no idea how to assign points to certain grade. 这些分数将在以后的计算中使用,但是我不知道如何将分数分配给某个年级。 I hope you understand my question and thank you for your help. 希望您能理解我的问题,并感谢您的帮助。

I had the same app for windows, there I just used something like: if(veryGood.IsSelected==true){points = 20}, and i want to achieve same effect here. 我在Windows上有相同的应用程序,在那里我只使用了类似的东西:if(veryGood.IsSelected == true){points = 20},我想在这里达到相同的效果。

You need a simple modifications on your function class: 您需要对函数类进行简单的修改:

public class function implements OnItemSelectedListener {

   private final static int[] grades = new int[]{20,40,60,80,100}; //change in whatever you want. The length of the array must be the same of the adapter, or an ArrayIndexOutOfBoundException may be throwed.

   @Override
   public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long id) {
       text.setText(grades[pos] + " points!");
   }
}

PS: in Java is a good practice to use upper case initial on class names eg Function instead of function. PS:在Java中,最好在类名上使用大写字母首字母,例如Function而不是function。

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

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