Android UI control series: Spinner (drop-down list)

黄舟
Release: 2023-03-05 06:12:01
Original
1348 people have browsed it

When registering an account on a website, the website will ask us to provide gender, birthday, city and other information. For convenience, a drop-down list is provided for us to choose. This function is also available on Android. This is Spinner drop-down list

While coding, we first need to time the Spinner component in the layout, then connect the optional content to the drop-down list through the ArrayAdapter, and finally to get the option selected by the user, we need to design an event listener setOnItemSelectedListener and implement onItemSelected to obtain the content selected by the user, and finally set the current display item through the setVisibility method

SpinnerTest.java

package org.hualang.Spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SpinnerTest extends Activity {
    /** Called when the activity is first created. */
        private static final String[] citys={"杭州","北京","成都","大连","深圳","南京"};
        private TextView text;
        private Spinner spinner;
        private ArrayAdapter<String> adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text=(TextView)findViewById(R.id.text);
        spinner=(Spinner)findViewById(R.id.spinner);

        //将可选内容与ArrayAdapter连接
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,citys);
        //设置下拉列表风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter添加到spinner中
        spinner.setAdapter(adapter);
        //添加Spinner事件监听
        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
        {

                        @Override
                        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                                // TODO Auto-generated method stub
                                text.setText("你所在的城市是:"+citys[arg2]);
                                //设置显示当前选择的项
                                arg0.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
                                // TODO Auto-generated method stub

                        }

        });
    }
}
Copy after login

main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
<TextView  
        android:id="@+id/text"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="您所在的城市"  
    />  
<Spinner  
        android:id="@+id/spinner"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerHorizontal="true"  
/>  
</LinearLayout>
Copy after login

Run results:

Android UI control series: Spinner (drop-down list)

Android UI control series: Spinner (drop-down list)

The above is the content of the Android UI control series: Spinner (drop-down list). For more related content, please pay attention to PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template