日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android之自定义AlertDialog的实现方法(一)

發布時間:2025/3/20 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android之自定义AlertDialog的实现方法(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(一)Dialog簡介

? ?Dialog是Android開發中需要經常使用的系統組件之一,AlertDialog可以顯示一個、兩個等多個按鈕,使用setMessage()方法可以只顯示字符串提示信息,當然用戶也可以自定義自己的AlertDialog。

  • 構造方法

  • ? ?

    更改主題

    2. ?常用方法

    (二)代碼實現

    MainActivity代碼

    package?com.example.mydialog; import?java.util.ArrayList; import?java.util.HashMap; import?android.app.Activity; import?android.app.AlertDialog; import?android.content.DialogInterface; import?android.os.Bundle; import?android.util.Log; import?android.view.Menu; import?android.widget.ListAdapter; import?android.widget.SimpleAdapter; public?class?MainActivity?extends?Activity?{ private?ArrayList<HashMap<String,?Object>>?listItem; protected?void?onCreate(Bundle?savedInstanceState)?{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); openDialog(); } @Override public?boolean?onCreateOptionsMenu(Menu?menu)?{ //?Inflate?the?menu;?this?adds?items?to?the?action?bar?if?it?is?present. getMenuInflater().inflate(R.menu.main,?menu); return?true; } public?void?openDialog()?{ new?AlertDialog.Builder(MainActivity.this).setTitle("自定義Dialog") .setIcon(R.drawable.albums) .setAdapter(getAdaper(),?new?DialogInterface.OnClickListener()?{ @Override public?void?onClick(DialogInterface?dialog,?int?which)?{ //?TODO?Auto-generated?method?stub //?獲取選中項的內容 Log.d("測試",?listItem.get(which).get("ItemManager") .toString()); } }).show(); } public?ListAdapter?getAdaper()?{ listItem?=?new?ArrayList<HashMap<String,?Object>>(); HashMap<String,?Object>?map1?=?new?HashMap<String,?Object>(); map1.put("ImageManager",?R.drawable.compose); map1.put("ItemManager",?"錄入"); listItem.add(map1); HashMap<String,?Object>?map2?=?new?HashMap<String,?Object>(); map2.put("ImageManager",?R.drawable.camera); map2.put("ItemManager",?"拍照"); listItem.add(map2); HashMap<String,?Object>?map4?=?new?HashMap<String,?Object>(); map4.put("ImageManager",?R.drawable.delete1); map4.put("ItemManager",?"刪除"); listItem.add(map4); SimpleAdapter?listItemAdapter?=?new?SimpleAdapter(this,?listItem, R.layout.mydialog, new?String[]?{?"ImageManager",?"ItemManager"?},?new?int[]?{ R.id.p_w_picpath,?R.id.text?}); return?listItemAdapter; } }mydialog.xml文件 <?xml?version="1.0"?encoding="utf-8"?> <RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal"android:gravity="center_vertical"><ImageView?android:id="@+id/p_w_picpath"android:layout_width="38dp"android:layout_height="38dp"/><TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical?="true"android:textColor="#000000"?/></RelativeLayout>

    效果如下:



    轉載于:https://blog.51cto.com/6687431/1421226

    總結

    以上是生活随笔為你收集整理的Android之自定义AlertDialog的实现方法(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。