2011-10-12 65 views
1

这可以工作,但是当活动开始时它会自动向Toasts发送'One',因为它是默认选中的。如何做到这一点,微调框包含一个默认值,在实际的对话框中没有,例如'请选择一个类别',或者至少使'one'不被自动选择。谢谢如何在android中创建一个微调器?

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 

     }}); 

回答

2

默认情况下,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(); 
    } 
} 
+0

感谢的xD金正日接受它时,它让我哈哈并且那是一种耻辱,它确实是这样的X3 –

+0

我也花了很多时间在这个寻找解决方法。如果有人展示了一种方法来做到这一点,我会很高兴。对我来说,不希望添加“选择一个类别”,因此它被吸引。 – Caner

+0

如何你无法选择任何东西onNothingSlected如何btw工作顺便说一句哈哈 –

0

您可以手动设置默认选择的项目微调。

 catagorySpinner.setSelection(2); 
0

这将使得它在点击微调器时改变了阵列适配器,使得原来的值已经消失;在这种情况下,“选择产品类别”

 //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 
      } 
     }); 
相关问题