资源的方式添加

安卓中的下拉框 Spinner可以通过xml string中配置

    <string-array name="spingarr">
<item>支付宝</item>
<item>微信</item>
</string-array>

布局文件

        <Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/spingarr"
android:id="@+id/payMode"
android:layout_weight="1">
</Spinner>

获取spinner选中的值

private Spinner payMode;
private int payNum;
private String strPay;

payMode = (Spinner)findViewById(R.id.payMode);
payMode.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
strPay = (String)payMode.getSelectedItem();
} @Override
public void onNothingSelected(AdapterView<?> parent) { }
});

代码的方式添加

        payMode = (Spinner) findViewById(R.id.payMode);

        //数据
data_list = new ArrayList<String>();
data_list.add("微信");
data_list.add("支付宝");
//适配器
arr_adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list);
//设置样式
arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//加载适配器
spinner.setAdapter(arr_adapter);
05-06 12:47