• andriod Spinner


    <?xml version="1.0" encoding="UTF-8"?>
    
        <LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    
        <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="下拉框1:" android:id="@+id/label"/>
    
        <Spinner android:layout_height="wrap_content" android:layout_width="150dip" android:id="@+id/spinner1" android:drawSelectorOnTop="false"/>
    
        <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="下拉框2:" android:id="@+id/label"/>
    
        <Spinner android:layout_height="wrap_content" android:layout_width="150dip" android:id="@+id/spinner2" android:drawSelectorOnTop="false"/>
    
        <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/ok" android:id="@+id/ok"/>
    
    </LinearLayout>
    package com.example.yanlei.mytk;
    
    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.Button;
    import android.widget.Spinner;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class MainActivity extends AppCompatActivity {
    
    
        private Spinner spinner1;
        private Spinner spinner2;
        private Button ok;
        private ArrayAdapter countiesAdapter;
        private String[] mCounties = {"beijing", "guangdong", "guangxi", "hunan"};
        private List<String> allCounties = new ArrayList<String>();
        private String result = "你选择的是:";
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            spinner1 = (Spinner) findViewById(R.id.spinner1);
            spinner2 = (Spinner) findViewById(R.id.spinner2);
            ok = (Button) findViewById(R.id.ok);
    
            for (int i = 0; i < mCounties.length; i++) {
                allCounties.add(mCounties[i]);
            }
    
            countiesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, allCounties);
            countiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(countiesAdapter);
    
            ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.counties, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner2.setAdapter(adapter);
    
            //单击第一个下拉按钮时,显示选择的值。
            spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapter, View view,
                                           int position, long id) {
                    // TODO Auto-generated method stub
                    String str = (String) spinner1.getAdapter().getItem((int) id);
                    setTitle(result + str);
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
    
                }
            });
    
            //单击第二个下拉按钮时,显示选择的值。
            spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapter, View view,
                                           int position, long id) {
                    String str = (String) spinner2.getAdapter().getItem(position);
                    setTitle(result + str);
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                }
            });
    
    
            //单击确定按钮,提取选择的值.
            ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTitle(result + spinner1.getSelectedItem() + "  - >>  " + spinner2.getSelectedItem());
                }
            });
    
        }
    }

    arrays.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="counties">
            <item>AAA</item>
            <item>BBB</item>
            <item>CCC</item>
            <item>DDD</item>
            <item>EEE</item>
        </string-array>
    </resources>
  • 相关阅读:
    webDriver API——第11部分Remote WebDriver
    webDriver API——第10部分Chrome WebDriver
    转:Java NIO系列教程(六) File Channel
    转:Java NIO系列教程(五) 通道之间的数据传输
    转:Java NIO系列教程(四) Scatter/Gather
    转:Java NIO系列教程(三) Buffer
    转:Java NIO系列教程(二) Channel
    Java-优秀博客推荐
    java.net.Socket/java.net.ServerSocket-TCP Socket编程
    转:Java NIO系列教程(一)Java NIO 概述
  • 原文地址:https://www.cnblogs.com/gisoracle/p/5257871.html
Copyright © 2020-2023  润新知