最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)下拉菜單Spinner效果

 更新時(shí)間:2016年04月29日 15:36:37   投稿:lijiao  
這篇文章主要介紹了Android實(shí)現(xiàn)下拉菜單Spinner效果,學(xué)習(xí)Spinner組件的使用方法,非常好用的一款組件,相當(dāng)于從下拉列表中選擇項(xiàng)目,感興趣的小伙伴們可以參考一下

Android 中下拉菜單,即如html中的<select>,關(guān)鍵在于調(diào)用setDropDownViewResource方法,以XML的方式定義下拉菜單要顯示的模樣

1.1.activity_main.xml

<?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="vertical" 
  tools:context="com.rj141.sb.kongjian.MainActivity"> 
 
  <LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="18dp" 
      android:text="請選擇您最喜歡的水果:" /> 
 
    <Spinner 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/spinner" /> 
  </LinearLayout> 
 
 
  <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="18dp" 
    android:id="@+id/tv" /> 
 
</LinearLayout> 

Spinner是下拉列表的組件

1.2.MainActivity.class

public class MainActivity extends AppCompatActivity { 
 
  private Spinner s; 
  String[] data=new String[]{"蘋果","雪梨","西瓜","葡萄","橙子","草莓"}; 
  private TextView tv; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
 
    tv= (TextView) this.findViewById(R.id.tv); 
    s= (Spinner) this.findViewById(R.id.spinner); 
    s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data)); 
    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
        String str=data[position]; 
        tv.setText("最喜歡的水果是:"+str); 
      } 
      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
      } 
    }); 
  } 
} 
s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data));android.R.layout.simple_list_item_1是指安卓自帶的下拉列表格式,data是數(shù)據(jù)源;
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()){..};是下拉列表的監(jiān)聽

效果圖:

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)掌握Android實(shí)現(xiàn)下拉菜單Spinner組件有所幫助。

相關(guān)文章

最新評論

玉山县| 田东县| 永登县| 株洲市| 永康市| 华蓥市| 商水县| 博湖县| 五大连池市| 湖北省| 洛隆县| 车险| 合川市| 故城县| 通化县| 贵溪市| 松溪县| 绵阳市| 连南| 饶河县| 上栗县| 古田县| 陵川县| 清流县| 庆元县| 乐亭县| 高雄县| 玉树县| 田东县| 从江县| 资溪县| 香港| 马边| 渝中区| 长汀县| 五峰| 班玛县| 大埔县| 西藏| 武冈市| 临沧市|