Android之DialogFragment

DialogFragment在Android中用于创建和管理与Fragment交互的对话框。它监听dialog的关闭事件,确保状态同步。实现DialogFragment可以重写onCreateView填充内容或onCreateDialog创建自定义dialog。例如,可以通过XML布局文件创建登录窗口,然后继承DialogFragment并重写onCreateDialog方法。通过不断学习和实践,可以更好地掌握DialogFragment的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:14px;">        DialogFragment是基于Fragment展示Dialog的窗口。对话框的控制(何时隐藏、显示、关闭)通过对应的Dialogment 的API来完成,而不是Dialog的API.</span></span>

         DialogFragment需要保证FragmentDialog的状态保持一致。它监听dialogdissmiss event,并且去处理自己的状态。这意味着应该调用 show(FragmentManager, String)或者show(FragmentTransaction, String)来添加一个DialogFragment的实例给UI层,并且它自主将removedialog消失的时候。

       实现这个类创建Dialog,需要通过重写法onCreateView(LayoutInflater, ViewGroup, Bundle)来填充dialog的内容,或者重写方法onCreateDialog(Bundle)来创建一个完全自定义的dialog

1.   重写onCreateView创建Dialog

(a)布局文件,一个登陆窗口的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="18sp"/>

        <EditText
            android:id="@+id/edit_logiName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="请输入名字"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/edit_logiPwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="请输入密码"/>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorPrimary"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00000000"
            android:text="取消"/>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#00000000"
            android:layout_weight="1"
            android:text="登录"/>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        />
</LinearLayout>
(b) 继承DialogFragmeng重写onCreateView方法

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.dia_login, container, false);

    return view;
}

(c) 测试展示

LoginDialog loginDialog = LoginDialog.newInstance();
loginDialog.show(getFragmentManager(), "dilog"); 


(d) 设置Dialog样式

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    mNum = getArguments().getInt("num");

    int style = DialogFragment.STYLE_NORMAL, theme = 0;

    switch (mNum) {
        case 1: style = DialogFragment.STYLE_NO_TITLE; break;
        case 2: style = DialogFragment.STYLE_NO_FRAME; break;
        case 3: style = DialogFragment.STYLE_NO_INPUT; break;
        case 4: style = DialogFragment.STYLE_NORMAL; break;
        case 5: style = DialogFragment.STYLE_NORMAL; break;
        case 6: style = DialogFragment.STYLE_NO_TITLE; break;
        case 7: style = DialogFragment.STYLE_NO_FRAME; break;
        case 8: style = DialogFragment.STYLE_NORMAL; break;
    }
    switch (mNum) {
        case 4: theme = android.R.style.Theme_Holo; break;
        case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
        case 6: theme = android.R.style.Theme_Holo_Light; break;
        case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
        case 8: theme = android.R.style.Theme_Holo_Light;
            break;
    }

   
2.重写onCreateDialog方法

(a) DIalog布局同上

(b) 继承DialogFragment,重写OnCreateDialog,创建Dialog

public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View view = inflater.inflate(R.layout.dia_login, null, false);

    initUI(view);

    setListener();

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(view);
    return builder.create();
(c) 在合适的位置展示Dialog
(d)设置Dialog样式

在XML中定义

<style name="DialogStyle" parent="@android:style/Theme.Dialog">  
	<item name="android:windowBackground">@drawable/settings_window</item>  
	<item name="android:windowFrame">@null</item>  
	<item name="android:windowNoTitle">true</item>  
	<item name="android:windowIsFloating">true</item>  
	<item name="android:windowIsTranslucent">true</item>  
	<item name="android:background">@android:color/background_light</item>  
	<item name="android:windowFullscreen">true</item>  
	<item name="android:backgroundDimEnabled">true</item>  
</style> 
创建Dialog时调用相应的style
public Dialog onCreateDialog(Bundle savedInstanceState) {  

*******
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.DialogStyle);

******
}


集思广益、坚持不懈的总结,总会进步!

参考资料:

        1.http://www.android-doc.com/reference/android/app/DialogFragment.html

2.https://github.com/codepath/android_guides/wiki/Using-DialogFragment

  3.http://blog.youkuaiyun.com/pipisky2006/article/details/8547539

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值