简体   繁体   中英

How to display different textview when Spinner option is selected in Android

I am developing an application and I need to show a differents Textview when the user take a each option of a Spinner would be select in Android Studio, my code is the next:

Txt_Tiempo = (Spinner) findViewById(R.id.Txt_Tiempo);

Txt_Tiempo.setAdapter(adapter);

        Txt_Tiempo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (i == 0) {

                }
                if (i == 1)
                {
                    Toast.makeText(Cesta_Activity.this, "En" , Toast.LENGTH_SHORT).show();
                }
                if (i == 2)
                {
                    Toast.makeText(Cesta_Activity.this, "Completa la orden y muy pronto nos pondremos en contacto contigo", Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

The Xml Code is:

When the first option of the Spinner is selected, a TextView should appear below the Spinner, but if option two is selected, a different TextView should appear, and so for all cases.

Add the textviews below your spinner and (Assuming you have set all the textviews' visibility to GONE) whenever (specific) option is selected you can set the respective textview visible by typing:

textview1.setVisibility(View.VISIBLE);

Result: 输出 gif 文件 Use following steps:

  1. Use all TextView at same position using ConstraintLayout or RelativeLayout .
  2. Set all TextView 's visibility = "gone" in .xml file.
  3. Then change visibility of every TextView when a new spinner item is selected.

Here is the activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/category1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="16dp"
        android:text="Category - 1"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/spinner"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="@+id/spinner"
        app:layout_constraintTop_toBottomOf="@+id/spinner"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/category2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="16dp"
        android:text="Category - 2"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/spinner"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="@+id/spinner"
        app:layout_constraintTop_toBottomOf="@+id/spinner"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/category3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="16dp"
        android:text="Category - 3"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/spinner"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="@+id/spinner"
        app:layout_constraintTop_toBottomOf="@+id/spinner"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/category4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="16dp"
        android:text="Category - 4"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/spinner"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="@+id/spinner"
        app:layout_constraintTop_toBottomOf="@+id/spinner"
        app:layout_constraintVertical_bias="0.0" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:spinnerMode="dropdown"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.32" />

</androidx.constraintlayout.widget.ConstraintLayout>

and here is the MainActivity.java file:

public class MainActivity extends AppCompatActivity {

    TextView category1, category2, category3, category4;

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

        category1 = findViewById(R.id.category1);
        category2 = findViewById(R.id.category2);
        category3 = findViewById(R.id.category3);
        category4 = findViewById(R.id.category4);

        Spinner spinner = findViewById(R.id.spinner);
        String[] spinnerArray = {"Category-1", "Category-2", "Category-3", "Category-4"};
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, spinnerArray);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerArrayAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int category, long id) {
                showTextView(category);
            }

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

            }
        });
    }

    void showTextView(int category) {
        switch (category) {
            case 0:
                category1.setVisibility(View.VISIBLE);
                category2.setVisibility(View.GONE);
                category3.setVisibility(View.GONE);
                category4.setVisibility(View.GONE);
                break;
            case 1:
                category1.setVisibility(View.GONE);
                category2.setVisibility(View.VISIBLE);
                category3.setVisibility(View.GONE);
                category4.setVisibility(View.GONE);
                break;
            case 2:
                category1.setVisibility(View.GONE);
                category2.setVisibility(View.GONE);
                category3.setVisibility(View.VISIBLE);
                category4.setVisibility(View.GONE);
                break;
            case 3:
                category1.setVisibility(View.GONE);
                category2.setVisibility(View.GONE);
                category3.setVisibility(View.GONE);
                category4.setVisibility(View.VISIBLE);
                break;
        }
    }
}

Enjoy your work now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM