常用属性:
1 2 3 4 5
| android:spinnerMode android:dropDownWidth="230dp" android:popupBackground="#66ccff" android:entries="@array/week" android:prompt
|
android:prompt 属性使用常见问题:
设置之后不起作用:prompt属性只有在dialog状态才有用,所以要在xml中,将style设置为Widget.Spinner
prompt属性要用string下资源,不支持字符直接输入,否则会报错误
两种写法
XML文件中设置数据源
1 2 3 4 5 6 7 8 9
| <Spinner android:id="@+id/spinner_arrays" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="50dp" android:dropDownWidth="230dp" android:entries="@array/week" android:popupBackground="#66ccff" android:spinnerMode="dropdown"></Spinner>
|
values/arrays.xml文件
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="week"> <item >星期一</item> <item >星期二</item> <item >星期三</item> <item >星期四</item> <item >星期五</item> <item >星期六</item> <item >星期天</item> </string-array> </resources>
|
代码中设置数据源
1 2 3 4 5
| <Spinner android:id="@+id/spinner_code" android:layout_width="match_parent" android:layout_height="wrap_content" android:spinnerMode="dropdown"></Spinner>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| codeSp = (Spinner) findViewById(R.id.spinner_code); final String[] arr={"深圳","上海","北京","山西"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,arr); codeSp.setAdapter(adapter);
codeSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(SpinnerTestActivity.this, "点击了" + arr[position], Toast.LENGTH_SHORT).show(); }
@Override public void onNothingSelected(AdapterView<?> parent) {
} });
|