如何在android中创建一个旋转器?
这是可行的,但是当活动启动时,它会自动提示“One”,因为它是默认选择的。如何使微调框包含实际对话框中不存在的默认值,例如“请选择一个类别”,或者至少不自动选择“一个”。谢谢
final String[] items = new String[] {"One", "Two", "Three"};
final Spinner catagorySpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Expense1.this,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catagorySpinner.setAdapter(adapter);
catagorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), items[position], Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}});
this works but when the activity starts it automatically toasts 'One' because it is selected by default. How to make it so the spinner box contains a default value that isnt in the actual dialog such as 'Please choose a catagory', or at least so that 'one' is not auto selected. Thanks
final String[] items = new String[] {"One", "Two", "Three"};
final Spinner catagorySpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Expense1.this,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catagorySpinner.setAdapter(adapter);
catagorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), items[position], Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果默认情况下未选择任何内容,Android 中的微调器默认显示适配器中的第一个值。不幸的是,没有办法改变它。
在您的情况下,您可以将
Choose a Category
添加到数组中:但是在
onItemSelected
中您必须处理这个问题,即:The spinner in Android by default shows the first value in the adapter if nothing is selected as default. There is no way to change it unfortunately.
In your case, you can add
Choose a Category
to your array:But inside
onItemSelected
you have to handle this, ie:您可以在微调器中手动设置默认选定的项目。
You can manually set the default selected item in the spinner.
这将使数组适配器在单击微调器时发生更改,从而使原来的值消失;在本例中“选择一个类别”
this will make it so the array adapter is changed when the spinner is clicked making it so the value that was origionally there is gone; in this case "select a catagory"