如何使用Spinner列表中的不同文本而不是Spinner的主文本?

时间:2017-01-28 11:21:45

标签: android

我有一个Spinner,用于为列表选择排序模式(最新,最旧等)。这些条目只有排序模式的名称(例如“最新”),但选中后,我希望Spinner的标题使用不同的字符串(例如“按最新排序”)。最好将结果字符串格式化为:

String selection = ...;
String titleText = getString(R.string.spinner_title, selection);

// In strings.xml
<string name="spinner_title">Sorting by %1$s</string>

我该怎么做?

1 个答案:

答案 0 :(得分:0)

为您的OnItemSelectedListener

创建Spinner()

任何尝试这个:

 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
   TextView selectedText = (TextView) parent.getChildAt(0);
   if (selectedText != null) {
      selectedText.setText("YOUR TEXT HERE"); //change text when item is selected
   }
}

您还可以更改spinner

的特定位置的文字