美文网首页
Android:Spinner用法

Android:Spinner用法

作者: 一米阳光给的温暖 | 来源:发表于2019-05-06 09:42 被阅读0次
    <!-- String.xml -->
    
    <resources>
        <string name="app_name">My Application</string>
        <string-array name="citys">
            <item>石家庄</item>
            <item>承德市</item>
            <item>唐山市</item>
            <item>秦皇岛市</item>
            <item>沧州市</item>
            <item></item>
        </string-array>
    </resources>
    
    
    // MainActivity.java
    
    package com.example.administrator.myapplication;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.accessibility.AccessibilityManager;
    import android.widget.AdapterView;
    import android.widget.Spinner;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        private Spinner spinner;
        private AdapterView.OnItemSelectedListener itemSelectedListener = new AdapterView.OnItemSelectedListener() {
            /**
             *
             * @param parent
             * @param view
             * @param position:
             *                被点击哪个
             * @param id
             */
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] datas = getResources().getStringArray(R.array.citys);
                Toast.makeText(MainActivity.this,datas[position], Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(MainActivity.this,"您来自火星", Toast.LENGTH_LONG).show();
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            spinner = (Spinner)findViewById(R.id.spinner);
            spinner.setOnItemSelectedListener(itemSelectedListener);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Android:Spinner用法

          本文链接:https://www.haomeiwen.com/subject/tubloqtx.html