Get the App
SLTechnology News&Howtos  ›  Development  › 

Android drop-down list box how to use Spinner

Shulou Source: shulou.com Published: 2022-05-31 21:28:54 09月10日 Update

This article mainly explains the "Android drop-down list box Spinner how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Android drop-down list box Spinner how to use" it!

The file directory is as follows:

Lay out a drop-down list box Spinner in activity_main.xml

Next, create the spinner_item.xml file under the layout folder, placing the controls in the drop-down list box, where only the text is displayed, so the code is as follows:

Then the call is made in MainActivity.java to set the data

Package com.example.administrator.myapplication; import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.Toast; public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) / / get control Spinner spinner = (Spinner) findViewById (R.id.spinner); / / data to be added to the drop-down list box String [] array = new String [] {"Tang Monk", "Sun WuKong", "Zhu Bajie", "Sha Wujing", "Little White Dragon"} / / create adapter final ArrayAdapter dataAdapter = new ArrayAdapter (MainActivity.this, R.layout.spinner_item, array); dataAdapter.setDropDownViewResource (R.layout.spinner_item); / / set adapter spinner.setAdapter (dataAdapter) for drop-down list box / / listening event spinner.setOnItemSelectedListener (new AdapterView.OnItemSelectedListener () {@ Override public void onItemSelected (AdapterView parent, View view, int position, long id) {String value = dataAdapter.getItem (position). ToString () / / get the value Toast.makeText of the selected drop-down list box item (MainActivity.this, "you selected:" + value, Toast.LENGTH_SHORT). Show ();} @ Override public void onNothingSelected (AdapterView parent) {/ / No selected processing events}});}}

The spinner completed for initializing the data is as follows:

The spinner of a selected item is as follows:

Thank you for your reading, the above is the "Android drop-down list box Spinner how to use" the content, after the study of this article, I believe you on the Android drop-down list box Spinner how to use this problem has a deeper understanding, the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Drop-down data files learning events content that is controls adapters adaptations code layouts ideas situations folders text articles more directories knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Linux vpn Apple