繁体   English   中英

从微调器中选择一个项目并将其显示在文本视图中

[英]select an item from a spinner and display it on a text view

如何从微调器中选择一个项目,然后在不使用按钮的情况下将其显示在文本视图中,我知道我必须使用onItemSelected(),但我不知道如何。 这是我的布局:

<Spinner
    android:id="@+id/selectSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:entries="@array/spinner_color">
</Spinner>

和我的textview是空的:

<TextView
    android:id="@+id/backgroundColorTV"
    android:text="@string/background_color_TV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
backgroundColorTV.setText(parent.getItemAtPosition(pos).toString());

将此行写入onItemSelected()方法。 像这样

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    backgroundColorTV.setText(parent.getItemAtPosition(position).toString());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM