Android Getting Started Tutorial | Use of DialogFragment

Pop-up windows are a common way of prompting.

img

DialogFragment was introduced in 3.0. It is a special Fragment used to display a modal dialog box on Activity.

DialogFragment example

Determine UI style

First we have to know what to make. Generally speaking, a simple pop-up window has a title and text content at one end. Or with a button or two.

Here we make a simple pop-up window with a title and text.

layout

After determining the style, write the layout first.

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>
Create a new pop-up window class

Create a new SimpleDialogclass inheritance DialogFragment.

  • existonCreate方法中接收传入的数据。传递数据使用了Bundle。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”; // 传输数据时用到的key public static final String K_CONTENT = “k_content”; private String title; private String content; @Override public 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 @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.dialog_simple, container, false); } @Override public 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); } }
  • In onCreateViewthe method, use the layout created above.
  • onViewCreatedPerform ui operations in methods .
use

Pop this window up. We use DialogFragment.show(@NonNull FragmentManager manager, @Nullable String tag)methods.

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("欢迎访问");

Run it on the machine and you can see the effect.

Summary :

Use DialogFragment to implement pop-up windows. You need to determine the ui style, create a layout, create a new class that inherits DialogFragment, and pass in data.

Share one last time

[Produced by Tencent Technical Team] Getting started with Android from scratch to mastering it, Android Studio installation tutorial + full set of Android basic tutorials

Android programming introductory tutorial

Java language basics from entry to familiarity

Insert image description here

Kotlin language basics from entry to familiarity

Insert image description here

Android technology stack from entry to familiarity

Insert image description here

Comprehensive learning on Android Jetpack

Insert image description here

For novices, it may be difficult to install Android Studio. You can watch the following video to learn how to install and run it step by step.

Android Studio installation tutorial

Insert image description here

With the Java stage of learning, it is recommended to focus on video learning at this stage and supplement it with book checking and filling in gaps. If you mainly focus on books, you can type the code based on the book's explanations, supplemented by teaching videos to check for omissions and fill in the gaps. If you encounter problems, you can go to Baidu. Generally, many people will encounter entry-level problems and give better answers.

You need to master basic knowledge points, such as how to use the four major components, how to create Service, how to layout, simple custom View, animation, network communication and other common technologies.

A complete set of zero-based tutorials has been prepared for you. If you need it, you can add the QR code below to get it for free.

A complete set of basic Android tutorials

Insert image description here

Insert image description here

Insert image description here

Insert image description here
Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/Android23333/article/details/132684580