Android微调器未显示选定值

时间:2016-11-18 00:05:29

标签: java android android-studio

下拉菜单可见,但不显示所选文本。但是,仍然会检索该值,因为我可以在单击它后对所选项目执行操作。

资源:

<resources>
<string name="app_name">Linear Circuits Toolkit</string>
<string-array name="band_1">
    <item>Black</item>
    <item>Brown</item>
    <item>Red</item>
    <item>Orange</item>
    <item>Yellow</item>
    <item>Green</item>
    <item>Blue</item>
    <item>Violet</item>
    <item>Grey</item>
    <item>White</item>
</string-array>
<string-array name="band_2">
    <item>Black</item>
    <item>Brown</item>
    <item>Red</item>
    <item>Orange</item>
    <item>Yellow</item>
    <item>Green</item>
    <item>Blue</item>
    <item>Violet</item>
    <item>Grey</item>
    <item>White</item>
</string-array>
<string-array name="band_3">
    <item>Black</item>
    <item>Brown</item>
    <item>Red</item>
    <item>Orange</item>
    <item>Yellow</item>
    <item>Green</item>
    <item>Blue</item>
    <item>Violet</item>
    <item>Gold</item>
    <item>Silver</item>
</string-array>
<string-array name="band_4">
    <item>Brown</item>
    <item>Red</item>
    <item>Green</item>
    <item>Blue</item>
    <item>Violet</item>
    <item>Grey</item>
    <item>Gold</item>
    <item>Silver</item>
</string-array>

活动XML:

<Spinner
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:id="@+id/spinner3"
    android:layout_below="@+id/textView7"
    android:layout_toRightOf="@+id/spinner2"
    android:layout_marginLeft="30dp"/>

<Spinner
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:id="@+id/spinner2"
    android:layout_toRightOf="@+id/spinner"
    android:layout_marginLeft="30dp"
    android:layout_below="@+id/textView7"/>

<Spinner
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:id="@+id/spinner4"
    android:layout_toRightOf="@+id/spinner3"
    android:layout_marginLeft="30dp"
    android:layout_below="@+id/textView7"/>

<Spinner
    android:layout_width="60dp"
    android:layout_height="26dp"
    android:id="@+id/spinner"
    android:layout_below="@+id/textView5"
    android:layout_marginLeft="10dp" />

活动java:

public class Resistor_Band extends AppCompatActivity {
String one="";
String two="";
String three="";
String four="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_resistor__band);

    final Spinner spinner1=(Spinner)findViewById(R.id.spinner);
    final Spinner spinner2=(Spinner)findViewById(R.id.spinner2);
    final Spinner spinner3=(Spinner)findViewById(R.id.spinner3);
    final Spinner spinner4=(Spinner)findViewById(R.id.spinner4);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,R.array.band_1,android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,R.array.band_2,android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter2);
    ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this,R.array.band_3,android.R.layout.simple_spinner_item);
    adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner3.setAdapter(adapter3);
    ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(this,R.array.band_4,android.R.layout.simple_spinner_item);
    adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner4.setAdapter(adapter4);
    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            one = parent.getItemAtPosition(position).toString();
            TextView tempView = (TextView) spinner1.getSelectedView().findViewById(android.R.id.text1);
            tempView.setTextColor(Color.BLACK);

        }

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

        }
    });
    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            two = parent.getItemAtPosition(position).toString();
            TextView tempView = (TextView) spinner2.getSelectedView().findViewById(android.R.id.text1);
            tempView.setTextColor(Color.BLACK);
        }

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

        }
    });
    spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            three = parent.getItemAtPosition(position).toString();
            TextView tempView = (TextView) spinner3.getSelectedView().findViewById(android.R.id.text1);
            tempView.setTextColor(Color.BLACK);
        }

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

        }
    });
    spinner4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            four = parent.getItemAtPosition(position).toString();
            TextView tempView = (TextView) spinner4.getSelectedView().findViewById(android.R.id.text1);
            tempView.setTextColor(Color.BLACK);
        }

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

        }
    });

感谢。

0 个答案:

没有答案