- 화면 디자인
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvName"
android:text="사용자 이름"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvEmail"
android:text="이메일"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="여기를 클릭"/>
</LinearLayout>
- dialog1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="사용자 이름"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dlgEdt1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="이메일"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dlgEdt2"/>
</LinearLayout>
- toast1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_star_big_on"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toastText1"
android:textSize="20dp"
android:text="TextView"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_star_big_on"/>
</LinearLayout>
- Java 코드
package com.cookandroid.project7_3;
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;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView tvName, tvEmail;
Button button1;
EditText dlgEdtName, dlgEdtEmail;
TextView toastText;
View dialogView, toastView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("사용자 정보 입력");
tvName = (TextView) findViewById(R.id.tvName);
tvEmail = (TextView) findViewById(R.id.tvEmail);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//dialog1.xml inflate - xml 파일을 가져와 '객체(여기서는 dialogView)'로써 사용
dialogView = (View) View.inflate(MainActivity.this, R.layout.dialog1, null);
//대화상자 생성 - AlertDialog.Builder
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("사용자 정보 입력");
dlg.setIcon(R.drawable.ic_menu_allfriends);
dlg.setView(dialogView);
//확인 클릭시
dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
//대화상자(dialog1.xml을 인플레이트 한 객체)에 입력한 이름과 이메일에 접근
dlgEdtName = (EditText) dialogView.findViewById(R.id.dlgEdt1);
dlgEdtEmail = (EditText) dialogView.findViewById(R.id.dlgEdt2);
//텍스트뷰의 텍스트를 위에서 가져온 이름과 이메일로 setting
tvName.setText(dlgEdtName.getText().toString());
tvEmail.setText(dlgEdtEmail.getText().toString());
}
});
//취소 클릭시
dlg.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast toast = new Toast(MainActivity.this);
//toast1.xml inflate
toastView = (View) View.inflate(MainActivity.this, R.layout.toast1, null);
toastText = (TextView) toastView.findViewById(R.id.toastText1);
toastText.setText("취소했습니다");
toast.setView(toastView);
toast.show();
}
});
dlg.show();
}
});
}
}
'Android > 5. 메뉴와 대화상자' 카테고리의 다른 글
Day 112 : 대화상자(Dialog) (0) | 2022.03.30 |
---|---|
Day 112 : 토스트(Toast) (0) | 2022.03.30 |
Day 112 : 메뉴 (실습) (0) | 2022.03.30 |