안드로이드 스튜디오 사용자 정보 입력 - andeuloideu seutyudio sayongja jeongbo iblyeog

activity_main

<?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"
android:gravity="center_horizontal">

<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="사용자 이름"/>
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이메일"/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</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:id="@+id/dlgEdt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="이메일"/>
<EditText
android:id="@+id/dlgEdt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>

toast.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="horizontal"
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:id="@+id/toastText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_star_big_on"/>
</LinearLayout>

MainActivity.java

package com.example.project0412;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
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.inflate(MainActivity.this,R.layout.dialog1,null);

AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("사용자 정보 입력");
dlg.setIcon(R.drawable.ic_menu_allfriends);
dlg.setView(dialogView);
dlg.setPositiveButton("확인",null);
dlg.setNegativeButton("취소",null);

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();

}

});

}
}