如何在android中创建一个旋转器?

发布于 2024-12-09 06:50:56 字数 986 浏览 5 评论 0原文

这是可行的,但是当活动启动时,它会自动提示“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分開簡單 2024-12-16 06:50:56

如果默认情况下未选择任何内容,Android 中的微调器默认显示适配器中的第一个值。不幸的是,没有办法改变它。

在您的情况下,您可以将 Choose a Category 添加到数组中:

final String[] items = new String[] {"Choose a category", "One", "Two", "Three"};

但是在 onItemSelected 中您必须处理这个问题,即:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if(position != 0)  {
        Toast.makeText(getApplicationContext(), items[position], Toast.LENGTH_SHORT).show();
    }
}

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:

final String[] items = new String[] {"Choose a category", "One", "Two", "Three"};

But inside onItemSelected you have to handle this, ie:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if(position != 0)  {
        Toast.makeText(getApplicationContext(), items[position], Toast.LENGTH_SHORT).show();
    }
}
红颜悴 2024-12-16 06:50:56

您可以在微调器中手动设置默认选定的项目。

        catagorySpinner.setSelection(2);

You can manually set the default selected item in the spinner.

        catagorySpinner.setSelection(2);
倚栏听风 2024-12-16 06:50:56

这将使数组适配器在单击微调器时发生更改,从而使原来的值消失;在本例中“选择一个类别”

        //final String selected;
        final int a; 
        final int x = 1;
        final ArrayList<String> items = new ArrayList<String>();
        items.add("Select A Category");
        final Spinner catagorySpinner = (Spinner) findViewById(R.id.spinner);
        final 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);

        final ArrayList<String> itemsTwo = new ArrayList<String>();
        itemsTwo.add("one"); itemsTwo.add("two"); itemsTwo.add("three");

        final ArrayAdapter<String> adapterTwo = new ArrayAdapter<String>(Expense1.this,
                    android.R.layout.simple_spinner_item, itemsTwo);
        adapterTwo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        /* on spinner click listener (not items inside) */
        catagorySpinner.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                    catagorySpinner.setAdapter(adapterTwo);
                            catagorySpinner.setSelection(a);
                    x++;
                }

                return false;
            }
        });

        catagorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
                // if the spinner has been opened or not 
                 if(x!=1){      
                     a = position;
                    //code to execute if spinner has been clicked and arrayAdapter has been updated
                    //in my case selected = myArray.get(position);

            } else {

                //code to execute if "choose a catagory" is still there
                //in my case selected = "novalue";
            }
        }

        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
            }
        });

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"

        //final String selected;
        final int a; 
        final int x = 1;
        final ArrayList<String> items = new ArrayList<String>();
        items.add("Select A Category");
        final Spinner catagorySpinner = (Spinner) findViewById(R.id.spinner);
        final 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);

        final ArrayList<String> itemsTwo = new ArrayList<String>();
        itemsTwo.add("one"); itemsTwo.add("two"); itemsTwo.add("three");

        final ArrayAdapter<String> adapterTwo = new ArrayAdapter<String>(Expense1.this,
                    android.R.layout.simple_spinner_item, itemsTwo);
        adapterTwo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        /* on spinner click listener (not items inside) */
        catagorySpinner.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                    catagorySpinner.setAdapter(adapterTwo);
                            catagorySpinner.setSelection(a);
                    x++;
                }

                return false;
            }
        });

        catagorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
                // if the spinner has been opened or not 
                 if(x!=1){      
                     a = position;
                    //code to execute if spinner has been clicked and arrayAdapter has been updated
                    //in my case selected = myArray.get(position);

            } else {

                //code to execute if "choose a catagory" is still there
                //in my case selected = "novalue";
            }
        }

        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
            }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文