-1

儀式の時点で、私が持っているのは 3 つのオプションを持つ 1 つのスピナーだけです。私がやりたいことは、オプション 1 が選択された場合、リスト 1 からランダムに選択され、オプション 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());

// 部屋を選択するためのスピナーのリスナー実装

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 に答える