弹窗,是常见的一种提醒形式。

DialogFragment是在3.0时引入的,是一种非凡的 Fragment,用于在 Activity 上展现一个模态的对话框。

DialogFragment 示例

确定UI款式

首先咱们得晓得做成什么样。一般来说简略的弹窗是一个题目,一端文字内容。 或者带有一两个按钮。

这里咱们做一个有题目和文字的简略弹窗。

layout

确定好款式后,先把 layout 写进去。

dialog_simple.xml

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="12dp">    <TextView        android:id="@+id/title_tv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:textColor="#111111"        android:textSize="16sp"        android:textStyle="bold"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <TextView        android:id="@+id/content_tv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:gravity="center"        android:textColor="#111111"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@id/title_tv" /></androidx.constraintlayout.widget.ConstraintLayout>
新建弹窗类

新建一个SimpleDialog类继承DialogFragment

  • onCreate办法中接管传入的数据。传递数据应用了Bundle。
  • onCreateView办法中,应用上文建设的layout。
  • onViewCreated办法中进行ui操作。

    import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import androidx.annotation.NonNull;import androidx.annotation.Nullable;import androidx.fragment.app.DialogFragment;public class SimpleDialog extends DialogFragment {public static final String K_TITLE = "k_title"; // 传输数据时用到的keypublic static final String K_CONTENT = "k_content";private String title;private String content;@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    Bundle in = getArguments();    if (in != null) {        title = in.getString(K_TITLE);        content = in.getString(K_CONTENT);    }}@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {    return inflater.inflate(R.layout.dialog_simple, container, false);}@Overridepublic void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {    super.onViewCreated(view, savedInstanceState);    TextView titleTv = view.findViewById(R.id.title_tv);    TextView contentTv = view.findViewById(R.id.content_tv);    titleTv.setText(title);    contentTv.setText(content);}}
应用

把这个窗口弹出来。咱们应用DialogFragment.show(@NonNull FragmentManager manager, @Nullable String tag)办法。

private void popSimpleDialog1(String title, String content) {        SimpleDialog dialog = new SimpleDialog();        Bundle bundle = new Bundle();        bundle.putString(SimpleDialog.K_TITLE, title);        bundle.putString(SimpleDialog.K_CONTENT, content);        dialog.setArguments(bundle);        dialog.show(getSupportFragmentManager(), "one-tag");    }    // 调用    popSimpleDialog1("欢送拜访");

运行到机器上能够看到成果。

小结:
应用 DialogFragment 来实现弹窗。 须要确定 ui 款式,建设 layout,新建类继承DialogFragment,传入数据。

Android零根底入门教程视频参考