Android中的Dialog
生活随笔
收集整理的這篇文章主要介紹了
Android中的Dialog
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1 AlertDialog
- 1.1 普通對話框的創建
- 2 自定義對話框
- 2.1 自定義對話框的創建步驟
- 3 PopupWindow
- 3.1 PopupWindow介紹
- 4 ArrayAdapter
- 4.1 ArrayAdapter的介紹及使用
Android中的常用對話框:
- AlertDialog
- 自定義Dialog
- PopupWindow
1 AlertDialog
1.1 普通對話框的創建
利用AlertDialog中的構建器(Builder)來完成。
下面看下創建AlertDialog的兩種方法:
還是第一種方法使用起來比較簡單。
2 自定義對話框
2.1 自定義對話框的創建步驟
先來看下自定義對話框的效果:
然后看下自定義對話框的創建步驟:
設置布局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>設置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);//為對話框設置布局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();}});} }創建并顯示Dialog非常簡單:
MyDialog md = new MyDialog(this,R.style.mydialog); md.show();3 PopupWindow
3.1 PopupWindow介紹
先來看一下PopupWindow的效果:
下面看一下PopupWindow的使用步驟:
下面看下動畫的類型,主要有移動、透明、縮放、旋轉幾種類型:
我們需要先自定義一個布局,布局的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="復制"android:textColor="#ffffff"android:gravity="center"android:background="#000000"/></LinearLayout>下面看一下PopupWindows的顯示:
//設置PopupWindowpublic void showPopupWindow(View view) {//準備彈窗所需要的視圖對象View v = LayoutInflater.from(this).inflate(R.layout.popup_layout,null);//1.實例化對象//參數1:用在彈窗中的View//參數2、3:彈窗的寬高//參數4(focusable):能否獲取焦點//注意:這里的單位是像素,而xml中我們使用的dp為單位final PopupWindow window = new PopupWindow(v,190,35,true);//2.設置(背景、動畫)//設置背景window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//設置能響應外部的點擊事件window.setOutsideTouchable(true);//設置能響應點擊事件window.setTouchable(true);//①創建動畫資源 ②創建一個style應用動畫資源 ③對當前彈窗的動畫風格設置為第二部的資源索引//演示的為移動動畫window.setAnimationStyle(R.style.translate_anim);//3.顯示//參數1(anchor):錨//參數2、3:相對于錨在x、y方向上的偏移量window.showAsDropDown(view,-190,0);//為彈窗中的文本添加點擊事件 findViewById直接調用只能找到當前Activity所對應的控件v.findViewById(R.id.choose).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this,"您點擊了選擇",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,"您點擊了全選",Toast.LENGTH_SHORT).show();window.dismiss();}});v.findViewById(R.id.copy).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this,"您點擊了復制",Toast.LENGTH_SHORT).show();window.dismiss();}});}下面看一下動畫資源和style的定義:
我們需要先創建anim文件夾,然后再創建相應的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的介紹及使用
數組適配器,只能用來顯示單一的文本。構造方法如下:
要實現的效果如下:
先來看下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="測試"android:layout_marginLeft="15dp"/> </LinearLayout>再來看下java代碼的實現:
private void showArrayDialog() {final String[] items = {"Java","Mysql","Android","HTML","C","JavaScript"};//數組適配器//參數1:環境//參數2:布局資源索引,指的是每一項數據所呈現的樣式android.R.layout.xxx//參數3:數據源 // ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,items);//參數3:int textviewid:指定文本需要放在布局中對應id文本控制的位置ArrayAdapter adapter = new ArrayAdapter(this,R.layout.array_item_layout,R.id.item_txt,items);AlertDialog.Builder builer = new AlertDialog.Builder(this).setTitle("請選擇")//參數1:適配器對象(對數據顯示樣式的規則制定器)//參數2:監聽器.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();}總結
以上是生活随笔為你收集整理的Android中的Dialog的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android中的Menu
- 下一篇: 腾讯视频起诉抖音侵权索赔一亿 到底发生了