Android-多个旋转器

时间:2012-05-27 23:28:14

标签: android android-layout android-widget spinner

我有这段代码:

package lijap.app.starcraft2counters;

import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class Settings extends Activity implements OnItemSelectedListener {
Spinner answertime;
Spinner gametime;
Spinner missesallowed;

String[] answerseconds = { "Unlimited", "1 second", "2 seconds", "3 seconds",
        "4 seconds", " 5 seconds" };

String[] gameminutes = { "Unlimited", "1 minute", "2 minutes", "3 minutes",
        "4 minutes", " 5 minutes" };

String[] numberofmisses = { "Unlimited", "5", "10", "15",
        "20", "25" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(Settings.this,
            android.R.layout.simple_spinner_item, gameminutes);

    answertime = (Spinner) findViewById(R.id.s_answertime);
    answertime.setAdapter(adapter);
    answertime.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub


}

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

}
}

我试图投入超过1个微调器而感到困惑。我是否共享阵列适配器,还是为每个适配器创建一个新适配器? onItemSelected怎么样?正如你从顶部的琴弦中看到的那样,我想要三个旋转器。我该怎么做?

我是Android新手,你可以说。感谢所有帮助 - Lijap

2 个答案:

答案 0 :(得分:14)

如果适配器调整了相同的信息,您可以在不同的Spinner之间共享适配器。显然,您的每个适配器都需要调整一组String,因此您需要为每个ArrayAdapter创建一个Spinner

单个OnItemSelectedListener适用于3 Spinners(只要您设置它们)。您可以在作为参数传递的getId()上调用AdapterView<?>,以了解哪个Spinner引发了此事件。

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    switch(arg0.getId()){
        case R.id.s_answertime:
            break;
        case ...
    }
}

答案 1 :(得分:4)

检查一下

yearDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        selectedyear = parentView.getSelectedItemPosition();
        drpyear      = yearDropdown.getSelectedItem().toString();

        //your code here

    }

    public void onNothingSelected(AdapterView<?> parentView) 
    {
        selectedyear = 0;
        //return;
    }
});

monthDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        //your code here


    }

    public void onNothingSelected(AdapterView<?> parentView) 
    {
        selectedmonth = 0;
        //return;
    }
});

dayDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        //your code here

    }

    public void onNothingSelected(AdapterView<?> parentView) 
    {
        selectedday = 0;
    }
});

您需要为每个微调器创建每个适配器。

   adapterYear = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item);
   yearDropdown.setAdapter(adapterYear);

   adapterMonth = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item);
   monthDropdown.setAdapter(adapterMonth);

   adapterDays = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item);
   dayDropdown.setAdapter(adapterDays);