<分区>
我有一个 Android 表单需要根据某些选择进行 self 更新。该表格目前由 2 个微调器(A 和 B)组成。在微调器 A 做出选择之前,微调器 B 不会创建。选择完成后,B 将显示在 View 中,其内容会根据 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));
}
...
}
这段代码工作正常。当从列表中选择“Activate”或“Deactivate”时,showListB()
会执行,这与 showListA()
创建新行的方式非常相似,其中包含标签和微调器。
问题是,默认情况下,“Activate”显示在执行 showListB()
的 Spinner 中,并且马上,表单的第二部分是基于“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;
}
这行得通...但我不希望列表中有第三个选项。我只是希望它在默认情况下为空白或显示某种“提示”文本,一旦按下它,它就不是列表中的一个选项。这可能吗?
谢谢
编辑:
xml内容:
<Spinner
android:id="@+id/spinnerAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>