【问题标题】:How to show just 3 characters in an Android Spinner?如何在 Android Spinner 中仅显示 3 个字符?
【发布时间】:2020-12-29 08:57:43
【问题描述】:

我正在开发一个 android 项目,并且我在工作日制作了一个 Spinner,现在我只想显示在 Spinner 中选择的任何一天的缩写(周一、周二、周三等),但全名下拉列表中的天数(星期一、星期二、星期三等)。 我该怎么做?我已经用默认的 xml 布局制作了最简单的 Spinner。

【问题讨论】:

    标签: android android-layout android-spinner


    【解决方案1】:

    注意:这可能无法编译,它只是一个“伪”,为您提供一种方法的想法。

    您创建一个普通的 Spinner(您也可以创建一个 CustomView,然后将逻辑放在那里,但我会选择最快的)

    <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />
    

    您可以创建一个string-array,日期为

    <string-array name="Weekdays"> 
            <item>Monday</item> 
            <item>Tuesday</item> 
            <item>Wednesday</item> 
            <item>Thursday </item> 
            <item>Friday</item> 
            <item>Saturday</item> 
            <item>Sunday</item> 
    </string-array> 
    
    <string-array name="WeekdaysAbbreviations">
            <item>Mon</item>
            <item>Tue</item>
            <item>Wed</item>
            <item>Thu</item>
            <item>Fri</item>
            <item>Sat</item>
            <item>Sun</item>
        </string-array>
    

    然后你得到如下的工作日

    val weekdays = resources.getStringArray(R.array.Weekdays)
    

    那么测试将是:

    
            val weekDays = resources.getStringArray(R.array.Weekdays)
            val abbreviationWeekDays = resources.getStringArray(R.array.WeekdaysAbbreviations)
            //find the spinner
            val spinner = findViewById<AppCompatSpinner>(R.id.spinner)
            //create an adapter with the weekdays
            val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, weekDays)
            //set the adapter
            spinner.adapter = adapter
            spinner.onItemSelectedListener = object :
                AdapterView.OnItemSelectedListener {
                override fun onItemSelected(
                    parent: AdapterView<*>,
                    view: View, position: Int, id: Long
                ) {
                    //find the first TextView of the Adapter
                    val selectedText = parent.getChildAt(FIRST_POSITION) as TextView
                    //As the abbreviationWeekDays and WeekDays have the same size, you can get the position selected on weebDays and then print what you have on abbreviationdays
                    //Another option would be selectedText.text = weekDays[position].substring(0,4)
                    selectedText.text = abbreviationWeekDays[position]
                }
    
                override fun onNothingSelected(parent: AdapterView<*>) {
                    //Nothing selected \o/
                }
            }
    

    输出:

    我还创建了一个Sample repository on my Github account

    【讨论】:

      【解决方案2】:

      使用substring()

      String str = "Monday";
      String firstThree = str.substring(0,4);
      System.out.println(firstThree);
      

      输出

      Mon
      

      【讨论】:

        【解决方案3】:

        您的 Spinner 可能如下所示

        spinner.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item,new String[]{"Monday","Tuesday","Wednesday"});
        

        根据您的问题,我猜您想要做的只是设置一个 OnItemSelectedListener,为微调器中的元素处理它并在您希望的任何视图中显示缩写形式

        【讨论】:

          猜你喜欢
          • 2017-10-19
          • 1970-01-01
          • 2011-03-01
          • 1970-01-01
          • 2019-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多