我正在尝试使用微调器来设置另外三个微调器的选择,这些微调器使用资源中的预定义字符串数组动态更新。
虽然我已经使用了来自 stackoverflow 的其他优秀建议来达到这一点,但我不确定为什么我的 3 个更新的微调器会在单击它们之前显示不可见的选项。
这是主要代码:
package com.example.bush;
import com.example.bush.R;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity implements OnItemSelectedListener{
public final static String NAME_KEY = "com.example.bush.NAME";
int pos1, pos2;
ArrayAdapter<CharSequence> adapter1, adp0,adp1;
Spinner spinner1,spinner2,spinner3,spinner4, spinner5;
EditText editText, num3, num4, num5, num6, num7, num8;
int[] route1,route2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner_h_route);
spinner3 = (Spinner) findViewById(R.id.spinner_h1);
spinner4 = (Spinner) findViewById(R.id.spinner_h2);
spinner5 = (Spinner) findViewById(R.id.spinner_h3);
num3 = (EditText) findViewById(R.id.num_h1);
num4 = (EditText) findViewById(R.id.num_h2);
num5 = (EditText) findViewById(R.id.num_h3);
editText = (EditText) findViewById(R.id.edit_name);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.route_array, android.R.layout.simple_spinner_item);adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
Log.i("Hostile Route Spinner", "spinner1");
spinner1.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("AAA", "OnItemSelected");
pos1=spinner1.getSelectedItemPosition();
Log.i("Spinner 1 Position", Integer.toString(pos1));
switch(pos1)
{
case 0:
adp0 = ArrayAdapter.createFromResource(this, R.array.first_array, android.R.layout.simple_spinner_item);
adp0.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner3.setAdapter(adp0);spinner4.setAdapter(adp0);spinner5.setAdapter(adp0);
break;
case 1:
adp1 = ArrayAdapter.createFromResource(this, R.array.second_array, android.R.layout.simple_spinner_item);
adp1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner3.setAdapter(adp1);spinner4.setAdapter(adp1);spinner5.setAdapter(adp1);
break;
}
}
public void onNothingSelected(AdapterView<?> arg0) {}
}
这是 .xml 表格布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrolltest.xml"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name_label"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="@string/label1"
android:width="0px"
android:gravity="center"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/edit_name"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:ems="10"
android:width="0px"
android:gravity="center"
android:layout_weight="1"
android:hint="@string/edit_message" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label_h_route"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="@string/label2"
android:width="0px"
android:gravity="center"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner_h_route"
android:width="0px"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:entries="@array/route_array" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Spinner
android:id="@+id/spinner_h1"
android:width="0px"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/num_h1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:ems="10"
android:width="0px"
android:gravity="center"
android:layout_weight="1"
android:inputType="number" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Spinner
android:id="@+id/spinner_h2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:width="0px"
android:gravity="center"
android:layout_weight="1" />
<EditText
android:id="@+id/num_h2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:width="0px"
android:gravity="center"
android:layout_weight="1" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Spinner
android:id="@+id/spinner_h3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:width="0px"
android:gravity="center"
android:layout_weight="1" />
<EditText
android:id="@+id/num_h3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:width="0px"
android:gravity="center"
android:layout_weight="1" />
</TableRow>
</TableLayout>
</ScrollView>
以下是字符串资源:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Bush Calc</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">Bush Calc</string>
<string name="edit_message">Hi there :)</string>
<string name="unit_prompt">Choose a unit</string>
<string name="route_hint">Choose the route.</string>
<string name="label1">Your name:</string>
<string name="label2">Attacking route:</string>
<string-array name="first_array">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
<item>Item 5</item>
</string-array>
<string-array name="second_array">
<item>Item 6</item>
<item>Item 7</item>
<item>Item 8</item>
<item>Item 9</item>
<item>Item 10</item>
</string-array>
<string-array name="route_array">
<item>First</item>
<item>Second</item>
</string-array>
</resources>
感谢您提供的任何帮助,以使这些选项在微调器上可见!