现在我所拥有的只有一个带有三个选项的微调器,我想要做的是如果选择了选项1然后从列表1中随机选择,选项2从列表2中选择,依此类推,我有一个编写的小部分代码足以声明微调器并用选择填充它,我从哪里开始?谢谢你提前!
这是我在xml上的代码:
<Spinner
android:id="@+id/alcohol"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:prompt="@string/prompt" />
然后这是我的.java文件:
setContentView(R.layout.activity_options);
// Set Alcohol Spinner
Spinner spinner = (Spinner) findViewById(R.id.alcohol);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.alcohol, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
谢谢大家!
答案 0 :(得分:1)
我认为你需要从spinner中选择项目。如果是这样,那就试试吧。
spinner.setOnItemSelectedListener(new listener_Of_spinner());
//用于选择房间的微调器的监听器实现
public static class listener_Of_spinner implements OnItemSelectedListener
{ static String getSelectedItem;
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id)
{
// By using this you can get the position of item which you have selected from the dropdown
getSelectedItem = (parent.getItemAtPosition(pos)).toString();
}
public void onNothingSelected(AdapterView<?> parent)
{
// Do nothing.
}
};
希望这可能有用