我能够以这种方式让Spinner成为行动吧; re / values / languages.xml中的项目数组
<string-array name="languages">
<item>Finnish</item>
<item>French</item>
<item>German</item>
<item>Slovakian</item>
<item>Polish</item>
</string-array>
在res / menu / main.xml
中 <item
android:id="@+id/menuSort"
android:actionLayout="@layout/spinner"
android:showAsAction="ifRoom"
android:title="@string/choose"/>
在res / layout / spinner.xml中
<Spinner
android:id="@+id/spinner"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:entries="@array/languages"/>
最后是活动课;
public class Base_Activity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
final Spinner spinner = new Spinner(this);
Log.i("DEBUG1", "CHECKPOINT1");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
Log.i("DEBUG2", "CHECKPOINT2");
spinnerArrayAdapter.setDropDownViewResource(0);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String items=spinner.getSelectedItem().toString();
Log.i("Selected item : ",items);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return true;
}
}
我没有得到的是关于旋转项目的ItemSelected没有任何反应。从我的xml文件中可以看出,我甚至不需要Adapter来获取微调器。但我正在使用适配器来获得OnItemSelected。请帮帮我这里发生了什么?
答案 0 :(得分:18)
尝试这可能有助于你
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
final Spinner spinner = (Spinner) menu.getItem(0).getActionView().findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String items = spinner.getSelectedItem().toString();
Log.i("Selected item : ", items);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
return super.onCreateOptionsMenu(menu);
}
答案 1 :(得分:-1)
如果您想要点击活动,则必须添加OnItemClickListener
而不是OnItemSelectedListener
。
只有选择项目或使用轨迹球或向上/向下箭头浏览视图时,您的方法才有效。如果您想要点击,请添加OnItemClickListener
并覆盖
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3)