【问题标题】:Use a Spinner to change a TextView Android使用 Spinner 更改 TextView Android
【发布时间】:2017-09-07 14:48:29
【问题描述】:

我正在尝试使用微调器选择来更改 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>

【问题讨论】:

  • 如果您也可以上传您的 xml 代码。可能是您可以在微调器中添加 onItemSelected 标记并从那里创建方法。

标签: java android textview spinner


【解决方案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

    }
});

【讨论】:

  • 使用此代码时,我收到 setOnItemSelectedListener“无法解析符号”/OnItemSelectedListener“无效声明”/覆盖“此处不允许注释”/getSelectedItem“无法解析方法”/AdapterView“预期表达式”的错误.我不确定如何处理这些错误?
  • 谢谢尼莱什!我得到了要编译的应用程序,现在每当我转到此视图时,我都会遇到应用程序崩溃的问题。
  • --------- 崩溃开始 09-07 16:45:15.526 2554-2554/com.example.newpc.fizzbuzz E/AndroidRuntime: FATAL EXCEPTION: main Process: com .example.newpc.fizzbuzz,PID:2554 java.lang.NullPointerException:尝试在 com.example.newpc.fizzbuzz 的空对象引用上调用虚拟方法“java.lang.Object android.widget.Spinner.getSelectedItem()” .activity_game$1.onItemSelected(activity_game.java:43) at android.widget.AdapterView.fireOnSelected(AdapterView.java:931) 这是它的开始(字符限制)
  • 不幸的是,我仍然在同一个地方崩溃。- 我可以在 Nilesh 的其他地方给你发消息吗?
  • 确定@bigmouthsmithers
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多