lin*_*sek 8 android listeners spinner
我有一个Android表单,需要根据某些选择更新自己.该表格目前由2个Spinners(A和B)组成.在完成Spinner A的选择之前,不会创建微调器B. 选择完成后,B将显示在视图中,并根据A的选择动态填充内容.这是我的代码:
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));
}
...
}
Run Code Online (Sandbox Code Playgroud)
此代码正常工作.当从列表中选择"激活"或"取消激活"时,showListB()执行与showListA()创建包含Label和Spinner的新行的方式非常相似.
问题在于,默认情况下,Spinner中会显示"Activate",它会立即执行showListB(),表单的第二部分将根据"Activate"选项创建.我能想到的唯一解决方法是向Spinner添加第三个字段,如下所示:
private static final String[] actionList = {"None", "Activate", "Deactivate" };
...
switch (position)
{
case SEL_NONE:
break;
case SEL_ACTIVATE:
case SEL_DEACTIVATE:
showListB(v);
break;
}
Run Code Online (Sandbox Code Playgroud)
这有效......但我不希望列表中有第三个选项.我只是希望它默认为空白或显示某种"提示"文本,一旦按下它就不是列表中的选项.这可能吗?
谢谢
编辑:
xml内容:
<Spinner
android:id="@+id/spinnerAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
我的data-sizes.xml:
<?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>
Run Code Online (Sandbox Code Playgroud)
在main.xml中:
<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" />
Run Code Online (Sandbox Code Playgroud)
在Java代码中:
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
}
}
Run Code Online (Sandbox Code Playgroud)
所以,虽然我可以看到2作为我的第一个默认项目,但除非用户实际选择它,否则不会发生任
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
66714 次 |
| 最近记录: |