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

Android實(shí)現(xiàn)帶圖標(biāo)的列表對(duì)話框

 更新時(shí)間:2017年12月26日 10:34:09   作者:光仔December  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶圖標(biāo)的列表對(duì)話框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

根據(jù)之前學(xué)的Android對(duì)話框技術(shù),來(lái)實(shí)現(xiàn)下面一個(gè)效果:界面有一個(gè)"打開設(shè)置對(duì)話框"按鈕,將彈出選擇項(xiàng)目的對(duì)話框,單擊任意列表項(xiàng),都將關(guān)閉該對(duì)話框,并通過(guò)消息提示框顯示選擇的列表內(nèi)容。

效果如圖所示:


實(shí)現(xiàn)代碼:
res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  android:orientation="vertical"  
  android:layout_width="fill_parent"  
  android:layout_height="fill_parent"  
  android:id="@+id/layout1" 
  >  
  <Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"  
    android:text="打開設(shè)置對(duì)話框"/> 
</LinearLayout>  

編寫用于布局列表項(xiàng)內(nèi)容的XML布局文件items.xml,在該文件中,采用水平線形布局管理器,并在該布局管理器中添加ImageView組件和一個(gè)TextView組件,分別用于顯示列表項(xiàng)中的圖標(biāo)和文字。
res/layout/items.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" > 
  <ImageView 
    android:id="@+id/image" 
    android:paddingLeft="10px" 
    android:paddingTop="20px" 
    android:paddingBottom="20px" 
    android:adjustViewBounds="true" 
    android:maxWidth="72px" 
    android:maxHeight="72px" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
  <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10px" 
    android:layout_gravity="center" 
    android:id="@+id/title"/> 
</LinearLayout> 

MainActivity:

package com.example.test;  
  
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 
  
public class MainActivity extends Activity {  
 
 
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
     
    int[] imageId=new int[]{R.drawable.in,R.drawable.stop, 
        R.drawable.setting,R.drawable.music,R.drawable.exit}; 
    final String[] title=new String []{"程序管理","保密設(shè)置","安全設(shè)置","郵件設(shè)置","鈴聲設(shè)置"}; 
    List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>(); 
    //通過(guò)for循環(huán)將圖片id和列表項(xiàng)文字放到map中,并添加到List集合中 
    for (int i = 0; i < imageId.length; i++) { 
      Map<String,Object> map=new HashMap<String,Object>(); 
      map.put("image", imageId[i]); 
      map.put("title", title[i]); 
      listItems.add(map); 
    } 
    final SimpleAdapter adapter=new SimpleAdapter(this,listItems, 
        R.layout.item,new String[]{"title","image"},new int[]{R.id.title,R.id.image}); 
     
    Button button=(Button)findViewById(R.id.button1); 
    button.setOnClickListener(new OnClickListener() { 
       
      @Override 
      public void onClick(View arg0) { 
        Builder builder=new AlertDialog.Builder(MainActivity.this); 
        builder.setIcon(R.drawable.music);//設(shè)置對(duì)話框的圖標(biāo) 
        builder.setTitle("設(shè)置:");//設(shè)置對(duì)話框的標(biāo)題 
        //添加列表項(xiàng) 
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() { 
           
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
            Toast.makeText(MainActivity.this, "您選擇了【"+title[which]+"】", Toast.LENGTH_SHORT).show(); 
             
          } 
        }); 
        builder.create().show();//創(chuàng)建對(duì)話框并顯示 
      } 
    }); 
  }  
}  

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

迁西县| 大田县| 邛崃市| 定日县| 安新县| 忻州市| 景宁| 资溪县| 昌吉市| 彰化县| 吐鲁番市| 红原县| 边坝县| 安阳县| 遂宁市| 千阳县| 延安市| 百色市| 龙口市| 宜黄县| 新竹县| 松桃| 中江县| 婺源县| 武乡县| 图木舒克市| 蕉岭县| 宁城县| 桃园市| 河津市| 阿合奇县| 姜堰市| 来宾市| 松桃| 思茅市| 大同县| 临清市| 大埔区| 邯郸县| 化隆| 上饶县|