0
0

【Bingで解説】「AlertDialog」の「listener」って何?

Posted at

あなたは知識が豊富で優秀なKotlinエンジニアです。

「AlertDialog」の
.setPositiveButton("Yes", null)
.setNegativeButton("No", null)

の第二引数の「listener」とはどういった役割を果たしますか?

「setPositiveButton」や「setNegativeButton」は、AlertDialog.Builderクラスのメソッドで、ダイアログに表示するボタンを設定するために使用されます。第一引数には、ボタンに表示するテキストを指定し、第二引数には、ボタンがクリックされたときに実行する処理を指定します。この第二引数に指定する処理を、リスナーと呼びます。

リスナーは、ボタンがクリックされたときに実行される処理を定義するために使用されます。リスナーを指定することで、ボタンがクリックされたときに、アプリケーションが必要な処理を実行することができます。

例えば、以下のように「setPositiveButton」を使用して、ダイアログに「Yes」というテキストを表示し、ボタンがクリックされたときに、アプリケーションが必要な処理を実行するように指定することができます。

.setPositiveButton("Yes") { dialog, which ->
    // Yesボタンがクリックされたときに実行する処理をここに記述する
}

以上が、「setPositiveButton」や「setNegativeButton」の第二引数である「listener」の役割についての説明です。ご参考になれば幸いです。

ソース: Bing との会話 2023/11/26
(1) Android中AlertDialog, AltertDialog.Builder的基本用法与 .... https://blog.csdn.net/walk_and_think/article/details/50612231.
(2) . https://bing.com/search?q=AlertDialog+setPositiveButton+setNegativeButton+listener.
(3) Androidで色々なDialogを出してみる #Android - Qiita. https://qiita.com/cozyk100/items/719de17bebd4591788b3.
(4) AlertDialogのボタンを押したときに、閉じる、閉じないを .... https://www.hiramine.com/programming/android/alertdialogclosecontrol.html.
(5) AlertDialog not displaying Neutral Button Icon - Stack Overflow. https://stackoverflow.com/questions/61420788/alertdialog-not-displaying-neutral-button-icon.
(6) undefined. https://qiita.com/ohwada/items/f95518b6948b271433a3.
(7) java - setPositiveButton (int, DialogInterface.OnClickListener .... https://stackoverflow.com/questions/49056801/setpositivebuttonint-dialoginterface-onclicklistener-in-the-type-alertdialog.
(8) すでに、create()した、AlertDalogでsetPositiveButtonの .... https://teratail.com/questions/76480.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0