ポップアップ ウィンドウは、プロンプトを表示する一般的な方法です。
DialogFragment は 3.0 で導入され、アクティビティ上でモーダル ダイアログ ボックスを表示するために使用される特別なフラグメントです。
DialogFragment の例
UI スタイルを決定する
まず何を作るかを知る必要があります。一般に、単純なポップアップ ウィンドウには、一方の端にタイトルとテキスト コンテンツがあります。またはボタンを 1 つまたは 2 つ使用します。
ここでは、タイトルとテキストを含む単純なポップアップ ウィンドウを作成します。
レイアウト
スタイルを決めたら、まずレイアウトを書きます。
ダイアログ_シンプル.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 が使用されます。import android.os.Bundle; android.view.LayoutInflaterをインポートします。android.view.Viewをインポートします。android.view.ViewGroup をインポートします。android.widget.TextViewをインポートします。androidx.annotation.NonNull をインポートします。androidx.annotation.Nullable をインポートします。androidx.fragment.app.DialogFragment をインポートします。public class SimpleDialog extends DialogFragment { public static Final String K_TITLE = “k_title”; // データ転送時に使用するキー public static Final String K_CONTENT = “k_content”; プライベート文字列タイトル。プライベート文字列コンテンツ。@Override public void onCreate(@Nullable Bundle SavedInstanceState) { super.onCreate(savedInstanceState); = getArguments(); でバンドルします。if (in != null) { title = in.getString(K_TITLE); コンテンツ = in.getString(K_CONTENT); @Nullable @Override public View onCreateView(@NonNull LayoutInflater インフレータ、@Nullable ViewGroup コンテナ、@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(タイトル); contentTv.setText(コンテンツ); } } - このメソッドでは
onCreateView
、上記で作成したレイアウトを使用します。 onViewCreated
メソッド内で ui 操作を実行します。
使用
このウィンドウをポップアップ表示します。私たちは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 スタイルを決定し、レイアウトを作成し、DialogFragment を継承する新しいクラスを作成して、データを渡す必要があります。
最後にもう一度シェアします
[Tencent 技術チーム制作] Android をゼロから始めてマスターする、Android Studio インストール チュートリアル + Android 基本チュートリアルのフルセット
Androidプログラミング入門チュートリアル
入門から慣れるまでの Java 言語の基礎
Kotlin 言語の基礎から入門まで
初心者から使い慣れたものまでの Android テクノロジースタック
Android Jetpack についての総合的な学習
初心者にとって、Android Studio のインストールは難しいかもしれません。次のビデオを見て、インストール方法と実行方法を段階的に学習できます。
Android Studioのインストールチュートリアル
Java の学習段階では、この段階ではビデオ学習に重点を置き、書籍を確認して穴埋めすることで補うことをお勧めします。書籍を中心に学習する場合は、書籍の説明に基づいてコードを入力し、指導ビデオで補足して抜け漏れを確認し、穴埋めすることができます。問題が発生した場合は、Baidu にアクセスすると、初級レベルの問題に遭遇した場合でも、より適切な回答が得られることが多いです。
4 つの主要コンポーネントの使用方法、Service の作成方法、レイアウト方法、簡単なカスタム View、アニメーション、ネットワーク通信、その他の一般的な技術などの基本的な知識を習得する必要があります。
ゼロベースのチュートリアルの完全なセットが用意されています。必要な場合は、以下の QR コードを追加して無料で入手できます。
基本的な Android チュートリアルの完全なセット