【问题标题】:How to create a spinner in android?如何在android中创建一个微调器?
【发布时间】:2011-10-14 08:30:27
【问题描述】:

这可行,但是当活动启动时,它会自动敬酒“一”,因为它是默认选中的。如何使微调框包含实际对话框中没有的默认值,例如“请选择一个类别”,或者至少不自动选择“一个”。谢谢

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

        }});

【问题讨论】:

    标签: android spinner adapter


    【解决方案1】:

    如果没有选择默认值,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();
        }
    }
    

    【讨论】:

    • 感谢 xD 我接受它,因为它让我哈哈,真可惜它这样做 x3
    • 我也花了很多时间来寻找解决方法。如果有人展示了这样做的方法,我会很高兴。对我来说,不希望添加“选择一个类别”,所以它很烂。
    • 如果你什么都选不了,onNothingSlected 是如何工作的哈哈
    【解决方案2】:

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

            catagorySpinner.setSelection(2);
    

    【讨论】:

      【解决方案3】:

      这将使数组适配器在单击微调器时更改,从而使原来的值消失;在这种情况下“选择一个类别”

              //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
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多