我正在尝试使用微调器选择来更改TextView。在我认为我的错误是,“onItemSelected”“永远不会被使用”。我是android / java的新手,所以我很难搞清楚为什么会发生这种情况。
public class activity_game extends AppCompatActivity {
public String myString1 = "Counter will increase every 3 seconds";
public TextView myCounterText;
private Spinner mySpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Spinner dropdown = (Spinner)findViewById(R.id.spinner);
String[] items = new String[]{"Select your difficulty!", "Easy", "Medium", "Hard"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long arg3) {
TextView myCounterText = (TextView)findViewById(R.id.myCounter);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
if (mySpinner.getSelectedItem().equals("Easy")){
myCounterText.setText("myString1");
}
}
public void toActivityPlay (View view) {
Intent toActivityPlay = new Intent(this, activity_play.class);
startActivity(toActivityPlay);
}
}
提前致谢。
编辑:这是我的XML(编辑了不必要的东西,以便我可以发布而不需要“主要是代码”。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
tools:context="com.example.newpc.fizzbuzz.activity_game">
<TextView
android:id="@+id/counterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="@string/counter_text"
android:textAlignment="center"
android:textColor="@color/text_shadow"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.229"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:id="@+id/startButton"
android:layout_width="239dp"
android:layout_height="73dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="@color/FizzBuzz_yellow"
android:text="@string/start_game"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="36sp"
android:textStyle="bold"
android:shadowColor="@color/text_shadow"
android:shadowDx="2"
android:shadowDy="2"
android:shadowRadius="3"
android:onClick="toActivityPlay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.863"
app:layout_constraintHorizontal_bias="0.531"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="240dp"
android:layout_height="28dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#ffffff"
android:dropDownWidth="match_parent"
android:entries="@+id/difficulty"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.137" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
尝试使用spinner的setOnItemSelectedListener
Spinner dropdown = (Spinner)findViewById(R.id.spinner);
String[] items = new String[]{"Select your difficulty!", "Easy", "Medium", "Hard"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String text=spin.getSelectedItem().toString();
if (text.equals("Easy")){
myCounterText.setText("myString1");
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});