1. AlertDialog에서 입력한 값을 MainActivity에 표출하기, AlertDIalog에서 취소버튼 클릭시 MainActivity에 Toast 띄우기
파일명 : 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"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="사용자 이름"
android:textSize="20dp" />
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이메일"
android:textSize="20dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="여기를 클릭" />
</LinearLayout>
파일명 : dialog1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="사용자 이름"
android:textSize="20dp" />
<EditText
android:id="@+id/dlgEdt1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이메일"
android:textSize="20dp" />
<EditText
android:id="@+id/dlgEdt2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
파일명 : toast1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/toastText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background"/>
</LinearLayout>
파일명 : MainActivity.java
package com.example.it.myapplication7_8;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
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 v) {
dialogView = (View) View.inflate(MainActivity.this, R.layout.dialog1, null);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("사용자 정보 입력");
dlg.setIcon(R.drawable.ic_launcher_background);
dlg.setView(dialogView);
dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dlgEdtName = (EditText) dialogView.findViewById(R.id.dlgEdt1);
dlgEdtEmail = (EditText) dialogView.findViewById(R.id.dlgEdt2);
tvName.setText(dlgEdtName.getText().toString());
tvEmail.setText(dlgEdtEmail.getText().toString());
}
});
dlg.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = new Toast(MainActivity.this);
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();
}
});
}
}
2. MainActivity에서 입력한 값을 AlertDialog에 표출하기
activity_main.xml, dialog1.xml, toast1.xml 은 위 파일과 같음
package com.example.it.myapplication7_9;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
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 {
EditText edtName, edtEmail;
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("사용자 정보 입력");
edtName = (EditText) findViewById(R.id.edtName);
edtEmail = (EditText) findViewById(R.id.edtEmail);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogView = (View) View.inflate(MainActivity.this, R.layout.dialog1, null);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("사용자 정보 입력");
dlg.setIcon(R.drawable.ic_launcher_background);
dlgEdtName = (EditText) dialogView.findViewById(R.id.dlgEdt1);
dlgEdtEmail = (EditText) dialogView.findViewById(R.id.dlgEdt2);
dlgEdtName.setText(edtName.getText().toString());
dlgEdtEmail.setText(edtEmail.getText().toString());
dlg.setView(dialogView);
dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dlgEdtName = (EditText) dialogView.findViewById(R.id.dlgEdt1);
dlgEdtEmail = (EditText) dialogView.findViewById(R.id.dlgEdt2);
edtName.setText(dlgEdtName.getText().toString());
edtEmail.setText(dlgEdtEmail.getText().toString());
}
});
dlg.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = new Toast(MainActivity.this);
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();
}
});
}
}
'IT > Android' 카테고리의 다른 글
android - Activity, Intent 응용 (액티비티 화면 세개 전환) (0) | 2018.09.22 |
---|---|
android - Activity, Intent (0) | 2018.09.22 |
android - AlertDialog, setItems, setSingleChoiceItems, setMultiChoiceItems (0) | 2018.09.21 |
android - AlertDialog 기본화면 (0) | 2018.09.21 |
android - Toast 위치 랜덤으로 표출하기 (0) | 2018.09.21 |