簡體   English   中英

在Android開發中使用Spinner

[英]using spinner in android development

我是android開發的新手,我對C#Windows編程有一些經驗,但也很少。 我正在使用使用微調器的android應用程序。 到目前為止,我找到了一個制作微調器項目的教程,並在您選擇微調器項目時在文本視圖中顯示微調器文本。 代碼看起來像這樣:

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);

    }

我想制作一個應用程序,它將根據用戶選擇的年級來計算一些分數。 示例:微調框將包含分數,例如負(1),正(2),好(3),非常好(4),優秀(5)。 我想要實現的是,當用戶選擇一個等級時,可以說非常好,該應用程序將非常好地使用該等級,即4,並將其轉換為我確定的一些點(1 = 20,2 = 40,3 = 60,4 = 80,5 = 100)。 這些分數將在以后的計算中使用,但是我不知道如何將分數分配給某個年級。 希望您能理解我的問題,並感謝您的幫助。

我在Windows上有相同的應用程序,在那里我只使用了類似的東西:if(veryGood.IsSelected == true){points = 20},我想在這里達到相同的效果。

您需要對函數類進行簡單的修改:

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:在Java中,最好在類名上使用大寫字母首字母,例如Function而不是function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM