LoginSignup
9
4

More than 3 years have passed since last update.

KotlinでAlertDialogを表示させる

Last updated at Posted at 2019-09-11

はじめに

KotlinでAlertDialogを勉強したので、簡単なアプリを作ってみました。
参考になれば幸いです。

作成したアプリ

4ez4c-l2muo.gif
EditTextに文字を入力し、Buttonをクリックすることで、その文字をTextViewに反映。何も文字が入力されていない時、AlertDialogを呼び出しダイアログを表示。

ダイアログの表示方法

MainActivity.kt
AlertDialog.Builder(this)
    .setTitle("ERROR!")
    .setMessage("入力してください")
    .setPositiveButton("OK"){ dialog, which -> }
    .show()

setTitle("ERROR!")でダイアログのタイトル文を挿入
setMessage("入力してください")でダイアログのメッセージ文を挿入
setPositiveButton("OK"){ dialog, which ->}でOKを選択した時の処理(今回は何も処理をさせず閉じるだけ)

ソースコード

MainActivity

MainActivity.kt
/* 
package、importなどは省略
*/
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener{
            if(editText.text.toString()==""){
                AlertDialog.Builder(this)
                    .setTitle("ERROR!")
                    .setMessage("入力してください")
                    .setPositiveButton("OK"){ dialog, which -> }
                    .show()
            }else{
                textView.text = editText.text.toString()
            }
        }
    }
}

activity_main.xml

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" 
            android:id="@+id/button" 
            android:layout_marginTop="32dp"
            app:layout_constraintTop_toBottomOf="@+id/editText" 
            app:layout_constraintHorizontal_bias="0.498"/>
    <EditText
            android:text="文字を入力"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" 
            android:id="@+id/editText"
            app:layout_constraintTop_toBottomOf="@+id/textView"
            android:layout_marginTop="32dp"/>
    <TextView
            android:text="文字を入力"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintEnd_toEndOf="parent"
            android:id="@+id/textView" 
            app:layout_constraintTop_toTopOf="parent" 
            android:layout_marginTop="128dp"
            android:textSize="30sp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

最後に

今回はAlertDialogのほんの一部にすぎません。ボタンを増やしたり、処理を書いたり、出来ることはまだまだたくさんあるなと思いました。レイアウトにこだわってsetIconでアイコン画像を貼り付けたり、setSingleChoiceItemsでラジオボタン選択したり、、、
また次の機会にでも挑戦したいと思います。

追記

図1.png
これは私が作成したアプリで使用したAlertDialogです。このようにsetSignalChoiceItemssetIconを用いることで、カスタマイズすることができます。

9
4
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
9
4