버튼 누르면 alertDiloge 창 열기

alertDiloge 창에 [제목, 설명, 텍스트 에딧, 확인, 취소.] 셋팅

alertDiloge 창에 확인 누르면 [텍스트 에딧]의 텍스트를 [텍스트 뷰]에 보여주기

xml 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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">
 
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="다이얼로그"
        android:id="@+id/btn_dilog"/>
    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="테스트"
        android:textSize="40sp" />
 
</LinearLayout>
 
 

 

java코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.example.dilogexample;
 
 
import android.content.DialogInterface;
 
public class MainActivity extends AppCompatActivity {
 
    Button btn_diloge;//버튼타입으로 btn_diloge 변수 선언
    TextView tv_result;//텍뷰타입으로 tv_result 변수 선언
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btn_diloge =(Button)findViewById(R.id.btn_dilog);
        //btn_diloge 에 넣어라= (버튼타입인)찾아서 뷰를 아이디로(리소스에. 아이디가. btn_diloge인 것을)
        tv_result = (TextView)findViewById(R.id.tv_result);
        //tv_result 에 넣어라= (텍뷰타입인)찾아서 뷰를 아이디로(리소스에. 아이디가. btn_diloge인 것을)
        btn_diloge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //버튼 누르면 실행해라
 
                //[알람다이얼로그]셋팅 시작
 
                AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
                //다이얼로그 빌더 ad 변수에 저장
                ad.setIcon(R.mipmap.ic_launcher);//아이콘 설정
                ad.setTitle("제목은요");//제목 설정
                ad.setMessage("글쓸꺼에요?");//메세지 설정
 
                final EditText et = new EditText(MainActivity.this);
                //et변수에 에딧텍스트 담기
                ad.setView(et);
                //ad에다가. 뷰를 셋팅해라. (et를)
 
                ad.setPositiveButton("확인"new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //확인 버튼을 만들고 누르면 실행해라
 
                        String result = et.getText().toString();
                        //스트링 타입 result 변수에  담아라=  et에서. 텍스트를 받아서. 스트링 타입으로
                        tv_result.setText(result);
                        //tv_result에. 세팅해라 텍스트를 (result의)
                        dialog.dismiss();
                        //다이얼로그 종료
 
                    }
                });
 
                ad.setNegativeButton("취소"new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //취소 버튼을 만들고 누르면 실행해라
                        
                        dialog.dismiss();
                        //다이얼로그 종료
 
                    }
                });
 
                ad.show();
                //[알람다이얼로그]셋탕한거 보여줘라
 
 
 
 
            }
        });
    }
 
}
 
 
 
Posted by 매드아이 Mad.I

댓글을 달아 주세요