안드로이드 다이얼로그 (AlertDialog)

 

317쪽

직접 풀어보기 7-3

1 쓰고

2 다이얼로그에서 값을 바꾸고 확인하면

3 바뀌고

4 취소 누르면 랜덤으로 토스창 뜸

 

 

 

activity_main.xml            

cancel.xml

dialogidpw.xml

 

 

MainActivity.java

package com.example.lhcdesktop.myapplication;

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.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText edtname,edtemail,a,b;
    Button btn;
    View dianameemail, cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtname =(EditText)findViewById(R.id.edtname);
        edtemail=(EditText)findViewById(R.id.edtemail);
        btn = (Button)findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
                dlg.setTitle("사용자 정보 입력");
                dianameemail=(View)View.inflate(MainActivity.this,R.layout.dialogidpw,null);
                 a= dianameemail.findViewById(R.id.dianame);
                 b =dianameemail.findViewById(R.id.diaemail);
                a.setText(edtname.getText().toString());
                b.setText(edtemail.getText().toString());
                dlg.setView(dianameemail);
                dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        edtname.setText(a.getText().toString());
                        edtemail.setText(b.getText().toString());
                    }
                });

                cancel=(View)View.inflate(MainActivity.this,R.layout.cancel,null);
                dlg.setNegativeButton("취소", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Display display =((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
                        int x=(int)(Math.random()*display.getWidth());
                        int y =(int)(Math.random()*display.getHeight());
                       Toast toast = new Toast(MainActivity.this);
                       toast.setGravity(Gravity.TOP|Gravity.LEFT,x,y);
                       toast.setView(cancel);
                       toast.show();

                    }
                });




                dlg.show();
            }
        });
    }
}

 

 

 

 

 

'■ Android > Practice' 카테고리의 다른 글

안드로이드 회원가입 화면  (0) 2019.11.22
안드로이드 로그인 화면  (0) 2019.11.21
중첩 리니어레이아웃(LinearLayout)  (0) 2018.10.22
안드로이드 주문  (0) 2018.10.17
안드로이드 계산기  (0) 2018.10.15