Android开发之Spinner

  • A+
所属分类:Android开发
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1" />

</LinearLayout>

 

 

MainActivity.java

package com.example.spinner;

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

public class MainActivity extends Activity {

    private StringBuffer stringBuffer;
    protected String str3;
    protected String str2;
    protected String str1;


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

        stringBuffer = new StringBuffer();

        //获取界面布局文件中的Spinner组件
        Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
        Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
        Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);

        //字符串数组
        String[] stringArr1 = {"机器人学院","智慧城市学院","特教学院","城市轨道交通学院"};
        String[] stringArr2 = {"软件工程","机械工程","机器人工程","人工智能"};
        String[] stringArr3 = {"2016级","2017级","2018级","2019级"};

        //创建ArrayAdapter对象
        ArrayAdapter<String> adapter1 = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,stringArr1);
        ArrayAdapter<String> adapter2 = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,stringArr2);
        ArrayAdapter<String> adapter3 = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,stringArr3);

        //为Spinner1设置Adapter
        spinner1.setAdapter(adapter1);

        //为Spinner1设置选中事件监听器
        spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                TextView tv = (TextView) view;
                str1 = tv.getText().toString();
                add();

            }

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

            }

        } );

        //为Spinner2设置Adapter
        spinner2.setAdapter(adapter2);

        //为Spinner2设置选中事件监听器
        spinner2.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                TextView tv = (TextView) view;
                str2 = tv.getText().toString();
                add();
            }

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

            }

        });

        //为Spinner3设置Adapter
        spinner3.setAdapter(adapter3);

        //为Spinner3设置选中事件监听器
        spinner3.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                TextView tv = (TextView) view;
                str3 = tv.getText().toString();
                add();

            }

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

            }

        });

    }


    protected void add() {
        // TODO Auto-generated method stub
        stringBuffer.delete(0, stringBuffer.length());
        show(str1,str2,str3);
        Toast.makeText(MainActivity.this, stringBuffer, Toast.LENGTH_LONG)
          .show();
    }


    private void show(String str1, String str2, String str3) {
        // TODO Auto-generated method stub
        stringBuffer.append(str1).append("--");
        stringBuffer.append(str2).append("--");
        stringBuffer.append(str3);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

效果截图

Android开发之Spinner

Android开发之Spinner

备注:开发环境为ADT

转载自:https://www.cnblogs.com/JQA576324/p/12740352.html

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: