【问题标题】:Text Color Changes to White when select item from searchable spinner从可搜索微调器中选择项目时,文本颜色变为白色
【发布时间】:2018-03-30 19:05:49
【问题描述】:

我在我的 android 应用程序中使用 custom 可搜索微调器。在我的一项活动中,我使用了两个可搜索的微调器。一个用于城市,一个用于区域位置。在选择第一个微调器项目时,我正在更改第二个微调器的适配器以显示相应城市的区域位置。但是当我选择第二个微调器的一个项目时,所选微调器项目的文本颜色变为白色。如何阻止它。

我在下面附上了屏幕截图和代码。

在选择任何项目之前

选择两个 Spinner 的项目后

Activity.java

citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            // Create an ArrayAdapter using the string array and a default spinner layout
            if(citySpinner.getItemAtPosition(i).equals("Mumbai"))
            {
                adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
                        R.array.mumbai, android.R.layout.simple_spinner_item);

                // Specify the layout to use when the list of choices appears
                adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                // Apply the adapter to the spinner
                areaSpinner.setAdapter(adapterArea);

            }
            else  if(citySpinner.getItemAtPosition(i).equals("Delhi"))
            {
                adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
                        R.array.delhi, android.R.layout.simple_spinner_item);

                // Specify the layout to use when the list of choices appears
                adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                // Apply the adapter to the spinner
                areaSpinner.setAdapter(adapterArea);

            }
            else  if(citySpinner.getItemAtPosition(i).equals("Thane"))
            {
                adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
                        R.array.thane, android.R.layout.simple_spinner_item);

                // Specify the layout to use when the list of choices appears
                adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                // Apply the adapter to the spinner
                areaSpinner.setAdapter(adapterArea);

            }
            else  if(citySpinner.getItemAtPosition(i).equals("Select City"))
            {
                buttonAdd.setEnabled(false);
                buttonAdd.setVisibility(View.GONE);
                adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
                        R.array.blank, android.R.layout.simple_spinner_item);
                // Specify the layout to use when the list of choices appears
                adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                // Apply the adapter to the spinner
                areaSpinner.setAdapter(adapterArea);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
    areaSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            if(areaSpinner.getItemAtPosition(i).equals("Select Area")|| areaSpinner.getItemAtPosition(i).equals("Select City First!"))
            {
                buttonAdd.setEnabled(false);
                buttonAdd.setVisibility(View.GONE);
            }
            else
            {
                buttonAdd.setEnabled(true);
                buttonAdd.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

Activity.xml

    <com.toptoche.searchablespinnerlibrary.SearchableSpinner
        android:id="@+id/spinnerCity"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_marginTop="24dp"
        android:entries="@array/city_name"
        app:hintText="Select City"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <com.toptoche.searchablespinnerlibrary.SearchableSpinner
        android:id="@+id/spinnerArea"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_marginTop="32dp"
        app:hintText="Select Area"
        app:layout_constraintEnd_toEndOf="@+id/spinnerCity"
        app:layout_constraintStart_toStartOf="@+id/spinnerCity"
        app:layout_constraintTop_toBottomOf="@+id/spinnerCity" />

【问题讨论】:

  • 是的,我在资源中为每个城市定义了数组。例如。 选择区域ChandivaliAndheriMalad选择区域DwarkaChandani ChowkAlipur
  • 你能在你定义数组的地方发布你的资源xml吗?
  • &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;string-array name="city_name"&gt; &lt;item&gt;Select City&lt;/item&gt; &lt;item&gt;Mumbai&lt;/item&gt; &lt;item&gt;Delhi&lt;/item&gt; &lt;item&gt;Thane&lt;/item&gt; &lt;/string-array&gt; &lt;string-array name="mumbai"&gt; &lt;item&gt;Select Area&lt;/item&gt; &lt;item&gt;Chandivali&lt;/item&gt; &lt;item&gt;Andheri&lt;/item&gt; &lt;item&gt;Malad&lt;/item&gt; &lt;/string-array&gt; &lt;string-array name="delhi"&gt; &lt;item&gt;Select Area&lt;/item&gt; &lt;item&gt;Dwarka&lt;/item&gt; &lt;item&gt;Alipur&lt;/item&gt; &lt;/string-array&gt; &lt;/resources&gt;
  • 尝试将 getApplicationContext() 替换为 getBaseContext()。希望有帮助!
  • @I_A_Mok ,现在颜色没有改变,但字体类型正在更改为系统默认值,我想使用自定义字体。你能帮帮我吗?

标签: android android-spinner


【解决方案1】:

更改您的创建适配器代码。 试试这个,

 ArrayAdapter  adapterArea=new ArrayAdapter(getBaseContext(),android.R.layout.simple_spinner_dropdown_item,getResources().getStringArray(R.array.mumbai));
    areaSpinner.setAdapter(adapterArea);

【讨论】:

  • Error:(105, 47): no suitable constructor found for ArrayAdapter(,int,String[]) constructor ArrayAdapter.ArrayAdapter(Context,int,int) 不适用(参数不匹配; 无法转换为 Context)构造函数 ArrayAdapter.ArrayAdapter(Context,int,Object[]) 不适用(参数不匹配; 无法转换为 Context)构造函数 ArrayAdapter.ArrayAdapter(Context,int ,List) 不适用(参数不匹配; 无法转换为 Context)
  • 问题在于您传递上下文的 OnItemSelectedListener 实例的第一个参数。
  • 我已经改成这个了,现在可以正常使用了,但是字体类型正在更改为系统默认值,我想使用自定义字体。 adapterArea = new ArrayAdapter(getBaseContext(),android.R.layout.simple_spinner_dropdown_item,getResources().getStringArray(R.array.mumbai));
  • 然后你需要为自定义字体创建样式并在微调器属性中设置。第二个你需要创建自定义布局并替换它,.
【解决方案2】:

在第一个微调器中,默认情况下从库中设置布局,但在第二个微调器中,您以编程方式设置它是 android 制造的,哪些颜色取决于您的项目主题,所以我建议您更改布局有一个定制的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    相关资源
    最近更新 更多