参见英文答案 >
How to make an Android Spinner with initial text “Select One”35个
我有一个Android表单需要根据某些选择更新自己.该表格目前由2个Spinners(A和B)组成.在完成Spinner A的选择之前,不会创建微调器B.选择完成后,B将显示在视图中,并根据A的选择动态填充内容.这是我的代码: @H_301_3@public class MyForm extends Activity { private final int SEL_ACTIVATE = 0; private final int SEL_DEACTIVATE = 1; private static final String[] actionList = {"Activate","Deactivate" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); table = (TableLayout) findViewById(R.id.table); showListA(table); } public void showListA(View v) { rowAction = new TableRow(this); Spinner spinner = new Spinner(this); spinner.setPrompt("Select..."); spinner.setOnItemSelectedListener( new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent,View v,int position,long id) { switch (position) { case SEL_ACTIVATE: case SEL_DEACTIVATE: showListB(v); break; } } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item,actionList); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner.setAdapter(adapter); rowAction.addView(tvAction); rowAction.addView(spinner); table.addView(rowAction,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); } ... }
我有一个Android表单需要根据某些选择更新自己.该表格目前由2个Spinners(A和B)组成.在完成Spinner A的选择之前,不会创建微调器B.选择完成后,B将显示在视图中,并根据A的选择动态填充内容.这是我的代码: @H_301_3@public class MyForm extends Activity { private final int SEL_ACTIVATE = 0; private final int SEL_DEACTIVATE = 1; private static final String[] actionList = {"Activate","Deactivate" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); table = (TableLayout) findViewById(R.id.table); showListA(table); } public void showListA(View v) { rowAction = new TableRow(this); Spinner spinner = new Spinner(this); spinner.setPrompt("Select..."); spinner.setOnItemSelectedListener( new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent,View v,int position,long id) { switch (position) { case SEL_ACTIVATE: case SEL_DEACTIVATE: showListB(v); break; } } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item,actionList); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner.setAdapter(adapter); rowAction.addView(tvAction); rowAction.addView(spinner); table.addView(rowAction,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); } ... }
此代码正常工作.当从列表中选择“激活”或“取消激活”时,showListB()执行与showListA()非常相似的创建包含Label和Spinner的新行.
问题是,默认情况下,Spinner中会显示“Activate”,执行showListB()并直接执行,表单的第二部分是基于“Activate”选项创建的.我能想到的唯一解决方法是向Spinner添加第三个字段,如下所示:
@H_301_3@private static final String[] actionList = {"None","Activate","Deactivate" }; ... switch (position) { case SEL_NONE: break; case SEL_ACTIVATE: case SEL_DEACTIVATE: showListB(v); break; }这有效……但我不希望列表中有第三个选项.我只是希望它默认为空白或显示某种“提示”文本,一旦按下它就不是列表中的选项.这可能吗?
谢谢
编辑:
xml内容:
@H_301_3@<Spinner android:id="@+id/spinnerAction" android:layout_width="wrap_content" android:layout_height="wrap_content"/>解决方法
我的data-sizes.xml:
@H_301_3@<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="chunks">
<item>2</item>
<item>4</item>
<item>8</item>
<item>16</item>
<item>32</item>
</string-array>
</resources>
在main.xml中:
@H_301_3@<Spinner android:id="@+id/spinnerSize" android:layout_marginLeft="50px" android:layout_width="fill_parent" android:drawSelectorOnTop="true" android:layout_marginTop="5dip" android:prompt="@string/SelectSize" android:layout_marginRight="30px" android:layout_height="35px" />在Java代码中:
@H_301_3@Spinner spinnerSize; ArrayAdapter adapter; ... spinnerSize = (Spinner)findViewById(R.id.spinnerSize); adapter = ArrayAdapter.createFromResource(this,R.array.chunks,android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerSize.setAdapter(adapter); spinnerSize.setOnItemSelectedListener(new MyOnItemSelectedListener()); ... class MyOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent,View view,int pos,long id) { chunkSize = new Integer(parent.getItemAtPosition(pos).toString()).intValue(); } public void onNothingSelected(AdapterView<?> parent) { // Dummy } }所以,虽然我可以看到2是我的第一个默认项目,但除非用户实际选择它,否则没有任何反应.
希望这可以帮助!