Android下拉列表示例含代碼

Android下拉列表示例

Android Spinner 類似于AWT或Swing的combox框。愛掏網(wǎng) - it200.com它用于向用戶顯示多個(gè)選項(xiàng),用戶只能選擇一個(gè)項(xiàng)目。愛掏網(wǎng) - it200.com

Android spinner類似于下拉菜單,其中有多個(gè)值供最終用戶選擇一個(gè)值。愛掏網(wǎng) - it200.com

Android spinner與AdapterView關(guān)聯(lián)。愛掏網(wǎng) - it200.com因此,您需要使用其中一個(gè)適配器類與spinner一起使用。愛掏網(wǎng) - it200.com

Android Spinner類是AsbSpinner類的子類。愛掏網(wǎng) - it200.com

在此示例中,我們將顯示國家列表。愛掏網(wǎng) - it200.com您需要使用 ArrayAdapter 類來存儲(chǔ)國家列表。愛掏網(wǎng) - it200.com

讓我們看一下Android中spinner的簡單示例。愛掏網(wǎng) - it200.com

activity_main.xml

從工具箱中拖動(dòng)Spinner,現(xiàn)在activity_main.xml文件將如下所示:

<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.javatpoint.com.spinner.MainActivity">  

    <Spinner  
        android:id="@+id/spinner"  
        android:layout_width="149dp"  
        android:layout_height="40dp"  
        android:layout_marginBottom="8dp"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="8dp"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintHorizontal_bias="0.502"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent"  
        app:layout_constraintVertical_bias="0.498" />  

</android.support.constraint.ConstraintLayout>  

Activity類

讓我們編寫代碼在下拉菜單中顯示項(xiàng)并進(jìn)行事件處理。愛掏網(wǎng) - it200.com

package example.javatpoint.com.spinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 implements
        AdapterView.OnItemSelectedListener {
    String[] country = { "India", "USA", "China", "Japan", "Other"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       //Getting the instance of Spinner and applying OnItemSelectedListener on it
        Spinner spin = (Spinner) findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(this);

        //Creating the ArrayAdapter instance having the country list
        ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //Setting the ArrayAdapter data on the Spinner
        spin.setAdapter(aa);

    }

    //Performing action onItemSelected and onNothing selected
    @Override
    public void onItemSelected(AdapterView arg0, View arg1, int position, long id) {
        Toast.makeText(getApplicationContext(),country[position] , Toast.LENGTH_LONG).show();
    }
    @Override
    public void onNothingSelected(AdapterView arg0) {
        // TODO Auto-generated method stub
    }
}

輸出:

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評(píng)論
更多 網(wǎng)友評(píng)論0 條評(píng)論)
暫無評(píng)論

返回頂部

主站蜘蛛池模板: 成人免费黄网站| 狠狠躁日日躁夜夜躁2022麻豆| 青青国产成人久久91网| 欧美亚洲一区二区三区| 成人在线不卡视频| 四虎影视久久久免费观看| 久久久久久久极品内射| 韩国福利影视一区二区三区| 日韩高清中文字幕| 国产婷婷一区二区三区| 亚洲色图13p| 99热国产在线观看| 波多野结衣系列痴女| 在线观看亚洲专区| 国产AV无码专区亚洲AV麻豆| 亚洲国产综合自在线另类| 2022国产麻豆剧果冻传媒入口| 欧美日韩精品一区二区三区不卡 | 99久久久精品免费观看国产| 理论亚洲区美一区二区三区| 日b视频免费看| 噗呲噗呲好爽轻点| 一本久久a久久精品vr综合| 精品一区二区三区在线观看视频 | 免费人成年激情视频在线观看| wwwjizzz| 欧美精品中文字幕亚洲专区| 女大学生沙龙室3| 同性女女黄h片在线播放| 一级一级一级一级毛片| 狠狠综合久久综合网站| 国产精品福利尤物youwu| 亚洲一区二区三区在线网站| 麻豆国产尤物AV尤物在线观看| 日日麻批免费40分钟无码| 国产成人综合久久久久久| 久久精品无码一区二区www| 色综合天天综合网国产成人网| 性生活大片免费看| 午夜欧美精品久久久久久久| chinese国产在线视频|