main.zip

 

액티비티를 팝업(Popup)으로 띄우기 , 데이터 주고받기

 

 

MainActivity.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
public class MainActivity extends AppCompatActivity {
 
    TextView txtResult;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        txtResult = (TextView)findViewById(R.id.txtResult);
    }
 
    //버튼
    public void mOnPopupClick(View v){
        //데이터 담아서 팝업(액티비티) 호출
        Intent intent = new Intent(this, PopupActivity.class);
        intent.putExtra("data""Test Popup");
        startActivityForResult(intent, 1);
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==1){
            if(resultCode==RESULT_OK){
                //데이터 받기
                String result = data.getStringExtra("result");
                txtResult.setText(result);
            }
        }
    }
}

cs

 
 

PopupActivity.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
public class PopupActivity extends Activity {
 
    TextView txtText;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //타이틀바 없애기
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.popup_activity);
 
        //UI 객체생성
        txtText = (TextView)findViewById(R.id.txtText);
 
        //데이터 가져오기
        Intent intent = getIntent();
        String data = intent.getStringExtra("data");
        txtText.setText(data);
    }
 
    //확인 버튼 클릭
    public void mOnClose(View v){
        //데이터 전달하기
        Intent intent = new Intent();
        intent.putExtra("result""Close Popup");
        setResult(RESULT_OK, intent);
 
        //액티비티(팝업) 닫기
        finish();
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //바깥레이어 클릭시 안닫히게
        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
            return false;
        }
        return true;
    }
 
    @Override
    public void onBackPressed() {
        //안드로이드 백버튼 막기
        return;
    }
}
cs

* 10 : 타이틀바 없애기

* 17-18 : Intent 에서 data 가져오기

* 25-27 : Intent로 data 전달하기

* 34-40 : 바깥 레이어를 눌러도 닫히지 않게 하기

* 43-46 : 안드로이드 백버튼 막기

 

 

activity_main.xml

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:paddingBottom="8dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="액티비티를 팝업으로 띄우기"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
 
    <LinearLayout
        android:paddingBottom="8dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:text="팝업"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mOnPopupClick"/>
    </LinearLayout>
 
    <LinearLayout
        android:paddingBottom="8dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/txtResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
 
</LinearLayout>
 
cs

 

 

popup_activity.xml

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#eeeeee"
    android:orientation="vertical"
    android:layout_width="300dp"
    android:layout_height="wrap_content">
 
    <!-- 타이틀바 -->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="Notice"
            android:textSize="20sp"
            android:textColor="#fff"
            android:gravity="center"
            android:background="#ff7a00"
            android:layout_width="match_parent"
            android:layout_height="53dp" />
    </LinearLayout>
    <!-- //end 타이틀바 -->
 
    <!-- Notice -->
    <LinearLayout
        android:padding="24dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/txtText"
            android:textSize="15sp"
            android:textColor="#000"
            android:alpha="0.87"
            android:gravity="center"
            android:layout_marginBottom="3dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <!-- Notice -->
 
    <View
        android:background="#66bdbdbd"
        android:layout_width="match_parent"
        android:layout_height="1dp" />
 
    <!-- 닫기 버튼 -->
    <LinearLayout
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:text="확인"
            android:textSize="15sp"
            android:textColor="#ff7a00"
            android:padding="16dp"
            android:gravity="center"
            android:background="#00000000"
            android:layout_width="match_parent"
            android:layout_height="53dp"
            android:onClick="mOnClose"/>
    </LinearLayout>
    <!--// 닫기 버튼 -->
</LinearLayout>
cs

팝업의 가로세로 길이는 직접 정해줍니다

 

 

AndroidMenifest.xml

1
2
        <!-- 팝업 Activity -->
        <activity android:name=".PopupActivity" android:theme="@android:style/Theme.Dialog" />
cs

* Popup으로 띄울 Activity의 Theme를 바꿔줍니다

 

 

결과

* 팝업 호출(데이터 전달) -> 팝업에서 전달받은 데이터 보여줌 , 팝업 닫기(데이터 전달) -> 데이터 받아서 보여줌

 



+ Recent posts