国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

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

2019-10-22 18:18:35
字體:
供稿:網(wǎng)友

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

效果如圖所示:

Android,圖標(biāo)列表,對話框,Android列表對話框,Android對話框

實(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è)置對話框"/> </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[]    List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();     //通過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è)置對話框的圖標(biāo)         builder.setTitle("設(shè)置:");//設(shè)置對話框的標(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)建對話框并顯示       }     });   }  }  

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 方正县| 六盘水市| 甘泉县| 库车县| 云林县| 鄱阳县| 阿坝县| 扶沟县| 阿勒泰市| 农安县| 哈尔滨市| 黎城县| 平顶山市| 齐河县| 井冈山市| 车险| 崇州市| 阿合奇县| 沂源县| 台南县| 东辽县| 民权县| 邢台县| 宿州市| 广汉市| 会东县| 奇台县| 江华| 崇礼县| 靖州| 扶余县| 和林格尔县| 延庆县| 十堰市| 东丰县| 台前县| 金阳县| 大埔区| 司法| 大姚县| 米脂县|