Dialog 를 이용하여 알림창 띄우는 방법
알림창을 띄우기 위해서 AlertDialog를 사용 하면 된다.
1. activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/DialogBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="알림창 띄우기"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
기본 버튼 하나 생성
2. MainActivity.java
package com.example.dialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button DialogBtn = (Button) findViewById(R.id.DialogBtn);
DialogBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setPositiveButton("닫기", new DialogInterface.OnClickListener() { // 버튼 생성
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.setTitle("알림창"); // 타이틀 설정
dialog.setMessage("메세지");// 메세지 설정
dialog.show(); //알림창 출력
}
});
}
}
AlertDialog 객체를 생성한뒤 set~ 명령어를 통해 알림 창을 설정 할 수 있다.
3. 출력화면
버튼 클릭시)
-정보를 입력받아 입력창으로 출력하기
1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="이름 : "
android:textSize="30dp"/>
<EditText
android:id="@+id/nameEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:hint="이름을 입력하십시오."
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="나이 : "
android:textSize="30dp"/>
<EditText
android:id="@+id/ageEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:hint="나이를 입력하십시오."
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="성별 : "
android:textSize="30dp"/>
<EditText
android:id="@+id/sexEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:hint="성별을 입력하십시오."
/>
</LinearLayout>
<Button
android:id="@+id/DialogBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="정보 보기"
android:layout_gravity="center"
/>
</LinearLayout>
2. MainActivity.java
package com.example.dialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText nameEdit = (EditText)findViewById(R.id.nameEdit);
EditText ageEdit = (EditText)findViewById(R.id.ageEdit);
EditText sexEdit = (EditText)findViewById(R.id.sexEdit);
Button DialogBtn = (Button) findViewById(R.id.DialogBtn);
DialogBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setPositiveButton("닫기", new DialogInterface.OnClickListener() { // 버튼 생성
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.setTitle("알림창"); // 타이틀 설정
dialog.setMessage("이름 : "+nameEdit.getText().toString()+System.lineSeparator()+
"나이 : " +ageEdit.getText().toString()+System.lineSeparator()+
"성별 : " +sexEdit.getText().toString()+System.lineSeparator());// 메세지 설정
dialog.show(); //알림창 출력
}
});
}
}
3. 실행 화면
버튼 클릭시)
'Android' 카테고리의 다른 글
Android] 갤러리에서 이미지 뷰로 이미지 불러오기 (0) | 2022.05.11 |
---|---|
Android] 액티비티를 팝업 창 모드로 출력하기 (1) | 2022.05.07 |
Android] Scroll View 사용 (0) | 2022.05.02 |
Android] Shared Preferences를 이용한 간단한 회원 가입 (0) | 2022.04.05 |
Android] 값을 입력 받아서 Gson 사용하기 (0) | 2022.04.01 |