Android-多纱厂(Android- Multiple Spinners)

2019-06-24 20:42发布

我有这样的代码:

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,你可以告诉。 所有帮助appreciated- Lijap

Answer 1:

您可以共享之间的不同的适配器Spinner ,如果他们适应了相同的信息秒。 显然,所有的适配器需要适应一套不同的String S,所以你需要创建一个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 ...
    }
}


Answer 2:

检查这一项

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


文章来源: Android- Multiple Spinners