当来自可搜索微调器的 select 项时,文本颜色变为白色
Text Color Changes to White when select item from searchable spinner
我在 android 应用程序中使用 custom 可搜索微调器。在我的 activity 之一中,我使用了两个可搜索的微调器。一种用于城市,一种用于区域位置。在第一个微调器项目的 selection 上,我正在更改第二个微调器的适配器以显示相应城市的区域位置。但是当我 select 第二个微调器的一个项目时,微调器的 selected 项目的文本颜色变为白色。如何阻止它。
我在下面附上了屏幕截图和代码。
在选择任何项目之前
选择两个微调器的项目后
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" />
在第一个微调器中,默认情况下从库中设置布局,但在第二个微调器中,您以编程方式设置它,这是 android 制作的,哪些颜色取决于您项目的主题,所以我的建议供您使用自定义布局更改布局。
更改您的创建适配器代码。
试试这个,
ArrayAdapter adapterArea=new ArrayAdapter(getBaseContext(),android.R.layout.simple_spinner_dropdown_item,getResources().getStringArray(R.array.mumbai));
areaSpinner.setAdapter(adapterArea);
我在 android 应用程序中使用 custom 可搜索微调器。在我的 activity 之一中,我使用了两个可搜索的微调器。一种用于城市,一种用于区域位置。在第一个微调器项目的 selection 上,我正在更改第二个微调器的适配器以显示相应城市的区域位置。但是当我 select 第二个微调器的一个项目时,微调器的 selected 项目的文本颜色变为白色。如何阻止它。
我在下面附上了屏幕截图和代码。
在选择任何项目之前
选择两个微调器的项目后
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" />
在第一个微调器中,默认情况下从库中设置布局,但在第二个微调器中,您以编程方式设置它,这是 android 制作的,哪些颜色取决于您项目的主题,所以我的建议供您使用自定义布局更改布局。
更改您的创建适配器代码。 试试这个,
ArrayAdapter adapterArea=new ArrayAdapter(getBaseContext(),android.R.layout.simple_spinner_dropdown_item,getResources().getStringArray(R.array.mumbai));
areaSpinner.setAdapter(adapterArea);