Android中的Dialog
文章目錄
- 1 AlertDialog
- 1.1 普通對(duì)話框的創(chuàng)建
- 2 自定義對(duì)話框
- 2.1 自定義對(duì)話框的創(chuàng)建步驟
- 3 PopupWindow
- 3.1 PopupWindow介紹
- 4 ArrayAdapter
- 4.1 ArrayAdapter的介紹及使用
Android中的常用對(duì)話框:
- AlertDialog
- 自定義Dialog
- PopupWindow
1 AlertDialog
1.1 普通對(duì)話框的創(chuàng)建
利用AlertDialog中的構(gòu)建器(Builder)來(lái)完成。
下面看下創(chuàng)建AlertDialog的兩種方法:
還是第一種方法使用起來(lái)比較簡(jiǎn)單。
2 自定義對(duì)話框
2.1 自定義對(duì)話框的創(chuàng)建步驟
先來(lái)看下自定義對(duì)話框的效果:
然后看下自定義對(duì)話框的創(chuàng)建步驟:
設(shè)置布局xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center_horizontal"android:background="@mipmap/dialog_bg"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="真的要退出嗎?"android:textSize="34sp"android:textColor="#e61414"android:textStyle="bold"android:layout_marginTop="265dp"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_margin="25dp"><Buttonandroid:id="@+id/yes_btn"android:layout_width="120dp"android:layout_height="50dp"android:background="@mipmap/yes_btn"/><Buttonandroid:id="@+id/no_btn"android:layout_width="120dp"android:layout_height="50dp"android:background="@mipmap/no_btn"android:layout_marginLeft="20dp"/></LinearLayout> </LinearLayout>設(shè)置style如下:
<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item></style><style name="mydialog" parent="android:style/Theme.Dialog"><item name="android:windowNoTitle">true</item><item name="android:windowBackground">@android:color/transparent</item></style> </resources>自定義Dialog:
package com.example.dialogdemo;import android.app.Dialog; import android.content.Context; import android.support.annotation.NonNull; import android.view.View;public class MyDialog extends Dialog {public MyDialog(@NonNull final Context context, int themeResId) {super(context, themeResId);//為對(duì)話框設(shè)置布局setContentView(R.layout.dialog_layout);findViewById(R.id.yes_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {System.exit(0);}});findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {dismiss();}});} }創(chuàng)建并顯示Dialog非常簡(jiǎn)單:
MyDialog md = new MyDialog(this,R.style.mydialog); md.show();3 PopupWindow
3.1 PopupWindow介紹
先來(lái)看一下PopupWindow的效果:
下面看一下PopupWindow的使用步驟:
下面看下動(dòng)畫的類型,主要有移動(dòng)、透明、縮放、旋轉(zhuǎn)幾種類型:
我們需要先自定義一個(gè)布局,布局的xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:background="#00ffff"android:padding="2dp"><TextViewandroid:id="@+id/choose"android:layout_width="60dp"android:layout_height="30dp"android:text="選擇"android:textColor="#ffffff"android:gravity="center"android:background="#000000"/><Viewandroid:layout_width="2dp"android:layout_height="30dp"android:background="#00ffff" /><TextViewandroid:id="@+id/choose_all"android:layout_width="60dp"android:layout_height="30dp"android:text="全選"android:textColor="#ffffff"android:gravity="center"android:background="#000000"/><Viewandroid:layout_width="2dp"android:layout_height="30dp"android:background="#00ffff" /><TextViewandroid:id="@+id/copy"android:layout_width="60dp"android:layout_height="30dp"android:text="復(fù)制"android:textColor="#ffffff"android:gravity="center"android:background="#000000"/></LinearLayout>下面看一下PopupWindows的顯示:
//設(shè)置PopupWindowpublic void showPopupWindow(View view) {//準(zhǔn)備彈窗所需要的視圖對(duì)象View v = LayoutInflater.from(this).inflate(R.layout.popup_layout,null);//1.實(shí)例化對(duì)象//參數(shù)1:用在彈窗中的View//參數(shù)2、3:彈窗的寬高//參數(shù)4(focusable):能否獲取焦點(diǎn)//注意:這里的單位是像素,而xml中我們使用的dp為單位final PopupWindow window = new PopupWindow(v,190,35,true);//2.設(shè)置(背景、動(dòng)畫)//設(shè)置背景window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//設(shè)置能響應(yīng)外部的點(diǎn)擊事件window.setOutsideTouchable(true);//設(shè)置能響應(yīng)點(diǎn)擊事件window.setTouchable(true);//①創(chuàng)建動(dòng)畫資源 ②創(chuàng)建一個(gè)style應(yīng)用動(dòng)畫資源 ③對(duì)當(dāng)前彈窗的動(dòng)畫風(fēng)格設(shè)置為第二部的資源索引//演示的為移動(dòng)動(dòng)畫window.setAnimationStyle(R.style.translate_anim);//3.顯示//參數(shù)1(anchor):錨//參數(shù)2、3:相對(duì)于錨在x、y方向上的偏移量window.showAsDropDown(view,-190,0);//為彈窗中的文本添加點(diǎn)擊事件 findViewById直接調(diào)用只能找到當(dāng)前Activity所對(duì)應(yīng)的控件v.findViewById(R.id.choose).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this,"您點(diǎn)擊了選擇",Toast.LENGTH_SHORT).show();window.dismiss(); //控制彈窗消失}});v.findViewById(R.id.choose_all).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this,"您點(diǎn)擊了全選",Toast.LENGTH_SHORT).show();window.dismiss();}});v.findViewById(R.id.copy).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this,"您點(diǎn)擊了復(fù)制",Toast.LENGTH_SHORT).show();window.dismiss();}});}下面看一下動(dòng)畫資源和style的定義:
我們需要先創(chuàng)建anim文件夾,然后再創(chuàng)建相應(yīng)的xml文件:
看下xml文件:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="0"android:toXDelta="0"android:fromYDelta="300"android:toYDelta="0"android:duration="2000"></translate> </set>然后看下style文件,如下:
<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item></style><style name="mydialog" parent="android:style/Theme.Dialog"><item name="android:windowNoTitle">true</item><item name="android:windowBackground">@android:color/transparent</item></style><style name="translate_anim"><item name="android:windowEnterAnimation">@anim/translate</item></style> </resources>4 ArrayAdapter
4.1 ArrayAdapter的介紹及使用
數(shù)組適配器,只能用來(lái)顯示單一的文本。構(gòu)造方法如下:
要實(shí)現(xiàn)的效果如下:
先來(lái)看下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="wrap_content"android:orientation="horizontal"android:padding="10dp"android:gravity="center_vertical"><ImageViewandroid:id="@+id/item_icon"android:layout_width="40dp"android:layout_height="40dp"android:src="@mipmap/star"/><TextViewandroid:id="@+id/item_txt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="測(cè)試"android:layout_marginLeft="15dp"/> </LinearLayout>再來(lái)看下java代碼的實(shí)現(xiàn):
private void showArrayDialog() {final String[] items = {"Java","Mysql","Android","HTML","C","JavaScript"};//數(shù)組適配器//參數(shù)1:環(huán)境//參數(shù)2:布局資源索引,指的是每一項(xiàng)數(shù)據(jù)所呈現(xiàn)的樣式android.R.layout.xxx//參數(shù)3:數(shù)據(jù)源 // ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,items);//參數(shù)3:int textviewid:指定文本需要放在布局中對(duì)應(yīng)id文本控制的位置ArrayAdapter adapter = new ArrayAdapter(this,R.layout.array_item_layout,R.id.item_txt,items);AlertDialog.Builder builer = new AlertDialog.Builder(this).setTitle("請(qǐng)選擇")//參數(shù)1:適配器對(duì)象(對(duì)數(shù)據(jù)顯示樣式的規(guī)則制定器)//參數(shù)2:監(jiān)聽器.setAdapter(adapter, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this,items[i],Toast.LENGTH_SHORT).show();dialogInterface.dismiss();}});builer.show();}總結(jié)
以上是生活随笔為你收集整理的Android中的Dialog的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android中的Menu
- 下一篇: 腾讯视频起诉抖音侵权索赔一亿 到底发生了