-1

截至目前,我所拥有的只是一个带有三个选项的微调器,我想做的是,如果选择了选项一,那么从 list1 中随机选择一个,选项 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);

谢谢大家!

4

1 回答 1

1

我认为从微调器中选择时需要获取该项目。如果是这样,请尝试此操作。

   spinner.setOnItemSelectedListener(new listener_Of_spinner());

// 用于选择房间的 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.
    }
};

希望这可能有用

于 2012-08-14T21:01:03.287 回答