동방프로젝트

실습 7-3)사용자 정보 입력 앱

두원공대88학번뚜뚜 2020. 8. 16. 01:14

package com.example.helloandroid;

import android.app.AlertDialog;
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;

import androidx.appcompat.app.AppCompatActivity;

@SuppressWarnings("deprecation")

//java코드만을 이용해 액션바 구성하기
public class MainActivity extends AppCompatActivity {
TextView tvName, tvEmail;
Button button1;
EditText dlgEdtName, dlgEdtEmail;
TextView toastText;
View dialogView, toastView;

@Override
public 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() {
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.mamang);
dlg.setView(dialogView);
dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dlgEdtName=(EditText)dialogView.findViewById(R.id.dlgEdit1);
dlgEdtEmail=(EditText)dialogView.findViewById(R.id.dlgEdit2);

tvName.setText(dlgEdtName.getText().toString());
tvEmail.setText(dlgEdtEmail.getText().toString());

//대화상자(in dialog1.xml)의 두 에디트 텍스트에 접근.
//주의할 점은, 인플레이트 한 dialogView.findViewById 메소드를 사용할 것
//이후, 대화상자에 입력한 이름과 이메일을 본화면(activity_main.xml)의 텍스트
//부에 적용
}
});
dlg.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast toast = new Toast(MainActivity.this);
toastView = (View) View.inflate(MainActivity.this, R.layout.toast, null);
toastText=(TextView)toastView.findViewById(R.id.toastText1);
toastText.setText("취소하였습니다");
toast.setView(toastView);
toast.show();
//역시, toast1.xml파일을 인플레이트 해 toastView에 대입
//setView()를 이용, 인플레이트 한 뷰를 토스트로 사용
//토스트의 텍스트뷰에 접근, 문자열을 "취소했습니다"로 변경, 그 후 토스트를 화면에 출력력
}
});
//dialog1.xml 파일을 인플레이트 해서 dialogView에 대입.
//이후 setView()로 인픞레이이트 한 뷰를 대화상자에 사용
//이를 통해, 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"
android:background="#ff0000"
android:gravity="center"
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tenmonan"/>
<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/chinhan"/>

</LinearLayout>

/////////////////////////

<?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/dlgEdit1"/>

<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/dlgEdit2"/>

</LinearLayout>

/////////////////

<?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: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:text="이메일"
android:id="@+id/tvEmail"/>

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

</LinearLayout>