android - 如何设置和从微调器获取值

时间:2015-09-07 12:19:18

标签: android spinner

我正在尝试动态设置和获取项目的微调器值?

任何想法?

我现在只需要Spinner行为的帮助,剩下的应该很容易。

3 个答案:

答案 0 :(得分:1)

Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.id.text1);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinnerAdapter.add("value");
spinnerAdapter.notifyDataSetChanged();
spinner.setSelection(0);
String text = spinner.getSelectedItem().toString();
  1. 1
  2. 2
  3. 3

答案 1 :(得分:0)

XML文件:

<Spinner android:id="@+id/Spinner01"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

Java文件:

公共类SpinnerExample扩展了Activity {

private String[] arraySpinner;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    this.arraySpinner = new String[] {
        "1", "2", "3", "4", "5"
    };
    Spinner s = (Spinner) findViewById(R.id.Spinner01);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, arraySpinner);
    s.setAdapter(adapter);

}

//从spenner获取价值     spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener(){     public void onItemSelected(AdapterView parent,View view,int pos,long id){           Object item = parent.getItemAtPosition(pos);       }       public void onNothingSelected(AdapterView parent){       } }); }

答案 2 :(得分:0)

试试这个会对你有帮助。

Spinner mySpinner= (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item);

mySpinner.setAdapter(myAdapter);

如果要动态添加元素,可以这样做:

myAdapter.add("newelement");
myAdapter.notifyDataSetChanged();