출처: https://3months.tistory.com/307 [Deep Play]

안드로이드/연습

[Android] Dialog

코딩하는 랄뚜기 2021. 8. 23. 14:20

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="대화상자"/>

</LinearLayout>

MainActivity.java

package com.example.dialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener(){
            public void onClick(View V){
                AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
                dlg.setTitle("제목입니다");
                dlg.setMessage("이곳이 내용입니다");
                dlg.setIcon(R.mipmap.ic_launcher);
                dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"확인을 눌렀네요",Toast.LENGTH_SHORT).show();
                    }
                });
                dlg.show();
            }
        });
    }
}

배운 점

대화상자의 기본 용도는 사용자에게 중요한 사항을 알려준 후, 사용자가 다시 어떤 선택을 하게 하는 것이 목적이라는 것을 인지하자.

 

실행화면

 

 

'안드로이드 > 연습' 카테고리의 다른 글

[Android] 사용자 정보 입력  (0) 2021.08.23
[Android] Dialog2  (0) 2021.08.23
[Android] ContextMenu  (0) 2021.08.23
[Android] Menu xml  (0) 2021.08.23
[Android] TapHost  (0) 2021.08.22