在 android 中获取选定微调器的文本
Get Text of Selected Spinner in android
我在 android 应用程序中使用微调器,如下所示
spino = dialog.findViewById(R.id.language_spinner);
if (spino != null) {
spino.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item_position = String.valueOf(position);
int itemposition = Integer.parseInt(item_position);
String selected = spino.getSelectedView().toString();
Log.e("selected position",""+itemposition);
Log.e("selected Text",selected);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
else{
Log.e("Selected item :","NULL:");
}
xml
布局如下
<Spinner
android:id="@+id/language_spinner"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:fontFamily="@font/popins_reguler"
android:entries="@array/languages"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:theme="@style/Spinner"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp">
获取所选位置工作正常,但我得到的所选位置文本如下所示
androidx.appcompat.widget.AppCompatTextView{dc81ebe V.ED..... ........ 0,35-986,111 #1020014 android:id/text1}
任何人都可以帮我获取微调器所选位置的文本吗?
谢谢!
要获取所选项目的文本,请使用 spinner.getAdapter().getItem(position)
spino = dialog.findViewById(R.id.language_spinner);
if (spino != null) {
spino.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item_position = String.valueOf(position);
int itemposition = Integer.parseInt(item_position);
String selected = String.valueOf(spino.getAdapter().getItem(position));
Log.e("selected position",""+itemposition);
Log.e("selected Text",selected);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
else{
Log.e("Selected item :","NULL:");
}