Android中Dialog對(duì)話框的使用小結(jié)
前言
最近有些空時(shí)間,所以花了一個(gè)小時(shí)對(duì)Dialog對(duì)話框使用小結(jié)一下,比較基礎(chǔ),希望對(duì)你學(xué)習(xí)有幫助,大牛請(qǐng)直接關(guān)閉網(wǎng)頁(yè)。如果你是新手,建議你親自敲一遍代碼。
先看一下效果:
Dialog對(duì)話框使用小結(jié)
一、普通對(duì)話框
AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("溫馨提示");//標(biāo)題 builder.setMessage("天氣冷,注意保暖"); builder.setIcon(R.mipmap.ic_launcher); builder.create(); builder.show();
普通對(duì)話框
二、確定取消對(duì)話框
builder.setTitle("確定取消對(duì)話框"); builder.setMessage("請(qǐng)選擇確定或取消"); builder.setIcon(R.mipmap.ic_launcher); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { //正能量按鈕 Positive @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "你點(diǎn)擊了確定", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "你選擇了取消", Toast.LENGTH_SHORT).show(); } }); builder.create().show();
確定取消對(duì)話框
三、多按鈕對(duì)話框
builder.setTitle("多個(gè)按鈕對(duì)話框"); builder.setMessage("請(qǐng)選擇"); builder.setIcon(R.mipmap.ic_launcher); builder.setPositiveButton("我沒玩夠", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "繼續(xù)瀏覽精彩內(nèi)容", Toast.LENGTH_SHORT).show(); } }); builder.setNeutralButton("開啟", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "起床了", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("我累了,要休息一下", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "歡迎再來", Toast.LENGTH_SHORT).show(); } }); builder.create().show();
多按鈕對(duì)話框
四、列表對(duì)話框
final String arrItem[] = getResources().getStringArray(R.array.aikaifa); builder.setItems(arrItem, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "你選擇了第" + arrItem[which], Toast.LENGTH_SHORT).show(); } }); builder.create().show();
列表對(duì)話框
五、帶Adapter的對(duì)話框
builder.setTitle("帶Adapter的對(duì)話框"); builder.setIcon(R.mipmap.ic_launcher); final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); int arrImg[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}; for (int i = 0; i < arrImg.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("img", arrImg[i]); map.put("title", "愛開發(fā)" + i); list.add(map); } SimpleAdapter adapter = new SimpleAdapter(activity, list, R.layout.list_item, new String[]{"img", "title"}, new int[]{R.id.iv, R.id.tv}); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "你選擇了" + list.get(which).get("title").toString().trim(), Toast.LENGTH_SHORT).show(); } }); builder.create().show();
帶Adapter的對(duì)話框
六、單選對(duì)話框
builder.setTitle("單選對(duì)話框"); builder.setIcon(R.mipmap.ic_launcher); builder.setSingleChoiceItems(R.array.aikaifa, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, which+"", Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show();
單選對(duì)話框
七、多選對(duì)話框
builder.setTitle("多選對(duì)話框"); builder.setIcon(R.mipmap.ic_launcher); builder.setMultiChoiceItems(R.array.aikaifa, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(activity, which+""+isChecked, Toast.LENGTH_SHORT).show(); } }); builder.create().show();
多選對(duì)話框
八、日期對(duì)話框
DatePickerDialog datePickerDialog=new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(activity, year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日", Toast.LENGTH_SHORT).show(); } }, 2017, 02, 9); datePickerDialog.show();
日期對(duì)話框
九、時(shí)間對(duì)話框
TimePickerDialog timePickerDialog=new TimePickerDialog(activity, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(activity, hourOfDay+"小時(shí)"+minute+"分鐘", Toast.LENGTH_SHORT).show(); } }, 17, 49, true); timePickerDialog.show();
時(shí)間對(duì)話框
十、自定義對(duì)話框
View view= LayoutInflater.from(activity).inflate(R.layout.dialog_login, null); builder.setView(view); builder.create(); final EditText et_phone=(EditText)view.findViewById(R.id.et_phone); final EditText et_password=(EditText)view.findViewById(R.id.et_password); Button btn_submit=(Button)view.findViewById(R.id.btn_submit); btn_submit.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(activity, "手機(jī)號(hào)碼:"+et_phone.getText().toString()+" 短信驗(yàn)證碼:"+et_password.getText().toString(), Toast.LENGTH_SHORT).show(); } }); builder.show();
自定義對(duì)話框
項(xiàng)目設(shè)計(jì)到的xml
list_item.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:background="#f5f5f5" android:orientation="horizontal" android:padding="10dp"> <ImageView android:id="@+id/iv" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv" android:text="標(biāo)題" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
dialog_login.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="8dp" android:orientation="vertical" android:padding="5dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:gravity="center_horizontal" android:text="驗(yàn)證手機(jī)號(hào)碼" android:textColor="#414141" /> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:hint="請(qǐng)輸入手機(jī)號(hào)碼" android:inputType="number" android:maxLength="11" android:paddingLeft="10dp" android:textSize="14sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:layout_marginTop="10dp"> <EditText android:id="@+id/et_password" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="4" android:gravity="center_vertical" android:hint="請(qǐng)輸入短信驗(yàn)證碼" android:inputType="number" android:maxLength="6" android:paddingLeft="10dp" android:textSize="14sp" /> <TextView android:id="@+id/tv_get_code" android:layout_width="0dp" android:layout_height="48dp" android:layout_marginLeft="10dp" android:layout_weight="2" android:enabled="false" android:gravity="center" android:text="點(diǎn)擊獲取" android:textColor="#000000" /> </LinearLayout> <Button android:id="@+id/btn_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:gravity="center" android:paddingBottom="10dp" android:paddingTop="10dp" android:text="提交" /> </LinearLayout> </RelativeLayout>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- 8種android 對(duì)話框(Dialog)使用方法詳解
- 屬于自己的Android對(duì)話框(Dialog)自定義集合
- Android中制作自定義dialog對(duì)話框的實(shí)例分享
- Android Dialog對(duì)話框詳解
- Android 對(duì)話框 Dialog使用實(shí)例講解
- Android Dialog對(duì)話框用法實(shí)例詳解
- Android Dialog 對(duì)話框詳解及示例代碼
- Android 對(duì)話框(Dialog)大全示例(建立你自己的對(duì)話框)
相關(guān)文章
Android JNI處理圖片實(shí)現(xiàn)黑白濾鏡的方法
這篇文章主要介紹了Android JNI處理圖片實(shí)現(xiàn)黑白濾鏡的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01Android table布局開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android table布局開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05android: targetSdkVersion升級(jí)中Only fullscreen activities can r
這篇文章主要給大家介紹了關(guān)于Android target SDK和build tool版本升級(jí)中遇到Only fullscreen activities can request orientation問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09Android RecyclerView詳解之實(shí)現(xiàn) ListView GridView瀑布流效果
RecyclerView 是Android L版本中新添加的一個(gè)用來取代ListView的SDK,它的靈活性與可替代性比listview更好2016-07-07Android開發(fā)重寫Animation實(shí)現(xiàn)下拉圖片后彈射回去效果示例
這篇文章主要介紹了Android開發(fā)重寫Animation實(shí)現(xiàn)下拉圖片后彈射回去效果,結(jié)合實(shí)例形式分析了Android自定義類繼承Animation實(shí)現(xiàn)圖片彈射效果的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10android activity設(shè)置無標(biāo)題實(shí)現(xiàn)全屏
本文將詳細(xì)介紹Android如何設(shè)置Activity全屏和無標(biāo)題的實(shí)現(xiàn)方法,需要的朋友可以參考下2012-12-12Android Studio / IDEA kotlin 顯示 var 真實(shí)類型操作
這篇文章主要介紹了Android Studio / IDEA kotlin 顯示 var 真實(shí)類型操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01