출처 : http://sharepid.tistory.com/988

 

안드로이드 커스텀 다이얼로그(Custom Dialog)로부터 값을 어떻게 입력받아야 하는지 알아보기 위한 포스팅...

여기서는 웹 사이트를 즐겨찾기에 추가하는 예제로 설명해보겠습니다.


# 커스텀 다이얼로그 레이아웃

dialog_favorite.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
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >
 
    <TableRow>
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="이름 : " />
 
        <EditText
            android:id="@+id/siteName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="여기에 사이트 이름을 입력하세요"
            android:selectAllOnFocus="true"
            android:singleLine="true" />
    </TableRow>
 
    <TableRow>
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="주소 : " />
 
        <EditText
            android:id="@+id/siteUrl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="여기에 사이트 주소를 입력하세요"
            android:selectAllOnFocus="true"
            android:singleLine="true" />
    </TableRow>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp" >
 
        <Button
            android:id="@+id/addOK"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="확인" />
 
        <Button
            android:id="@+id/addCancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="취소" />
    </LinearLayout>
 
</TableLayout>

# 메인 액티비티 레이아웃

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
45
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >
 
    <TableRow>
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="이름 : " />
 
        <TextView
            android:id="@+id/addSiteName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="여기에 입력받은 사이트 이름이 표시됩니다."
            android:selectAllOnFocus="true"
            android:singleLine="true" />
    </TableRow>
 
    <TableRow>
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="주소 : " />
 
        <TextView
            android:id="@+id/addSiteUrl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="여기에 입력받은 사이트 주소가 표시됩니다."
            android:selectAllOnFocus="true"
            android:singleLine="true" />
    </TableRow>
 
    <Button
        android:id="@+id/showDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="다이얼로그 띄우기" />
 
</TableLayout>


# 커스텀 다이얼로그 코드

FavoriteDialog.java

Dialog를 상속받는 클래스를 하나 만듭니다. context를 매개변수로 받는 생성자가 반드시 필요하고 onCreate() 메소드에서 setContentView() 메소드를 호출하여 방금 전 만든 커스텀 다이얼로그 레이아웃을 생성해야 합니다.

getSiteName()과 getSiteUrl() 메소드는 메인 액티비티에 값을 넘겨주기 위해 임의로 만든 메소드입니다.
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
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.*;
import android.view.View.*;
import android.widget.*;
 
public class FavoriteDialog extends Dialog implements OnTouchListener {
 
    private EditText siteName, siteUrl;
    private Button addOK, addCancel;
    private String _siteName, _siteUrl;
     
    public FavoriteDialog(Context context) {
        super(context); // context 객체를 받는 생성자가 반드시 필요!
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_favorite); // 커스텀 다이얼로그 레이아웃
 
        siteName = (EditText) findViewById(R.id.siteName);
        siteUrl = (EditText) findViewById(R.id.siteUrl);
        addOK = (Button) findViewById(R.id.addOK);
        addCancel = (Button) findViewById(R.id.addCancel);
 
        addOK.setOnTouchListener(this);
        addCancel.setOnTouchListener(this);
    }
 
    public String getSiteName() {
        return _siteName; // 사이트 이름을 반환하는 메소드
    }
 
    public String getSiteUrl() {
        return _siteUrl; // 사이트 주소를 반환하는 메소드
    }
 
    @Override // 터치 리스너
    public boolean onTouch(View v, MotionEvent event) {
        // 확인 버튼을 클릭하면 입력한 값을 적절히 설정한 후 다이얼로그를 닫음
        if (v == addOK) {
            _siteName = siteName.getText().toString(); // 입력된 사이트 이름을 얻어옴
            _siteUrl = siteUrl.getText().toString(); // 입력된 사이트 주소를 얻어옴
            dismiss(); // 이후 MainActivity에서 구현해준 Dismiss 리스너가 작동함
        }
 
        // 취소 버튼을 클릭하면 단순히 다이얼로그를 닫음
        else if (v == addCancel)
            cancel(); // 이후 MainActivity에서 구현해준 Dismiss와 Cancel 리스너가 작동함
         
        return false;
    }
}

# 메인 액티비티 코드

MainActivity.java

커스텀 다이얼로그 객체 생성 후 Dismiss 리스너와 Cancel 리스너를 달아줬는데요. 이는 커스텀 다이얼로그에서 각각 dismiss() 메소드, cancel() 메소드가 호출되었을 때 작동됩니다. 참고로 cancel() 메소드가 호출되었을 때 Dismiss, Cancel 메소드 둘 다 작동...
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
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.view.*;
import android.view.View.*;
import android.widget.*;
 
public class MainActivity extends Activity {
 
    TextView addSiteName, addSiteUrl;
    Button showDialog;
    FavoriteDialog favoriteDialog;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        addSiteName = (TextView) findViewById(R.id.addSiteName);
        addSiteUrl = (TextView) findViewById(R.id.addSiteUrl);
        showDialog = (Button) findViewById(R.id.showDialog);
 
        // 커스텀 다이얼로그 객체 생성
        favoriteDialog = new FavoriteDialog(MainActivity.this);
        favoriteDialog.setTitle("즐겨찾기 추가"); // 다이얼로그 타이틀 설정
         
        // 커스텀 다이얼로그 객체에 Dismiss 리스너 설정
        // 커스텀 다이얼로그가 사라졌을 때 취할 행동들
        favoriteDialog.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface arg0) {
                addSiteName.setText(favoriteDialog.getSiteName()); // 커스텀 다이얼로그로부터 사이트 이름을 얻어옴
                addSiteUrl.setText(favoriteDialog.getSiteUrl()); // 커스텀 다이얼로그로부터 사이트 주소를 얻어옴
                Toast.makeText(getApplicationContext(), "즐겨찾기를 추가하였습니다.", 1000).show();
            }
        });
 
        // 커스텀 다이얼로그 객체에 Cancel 리스너 설정
        // 커스텀 다이얼로그의 취소 버튼을 터치했을 때 취할 행동들
        favoriteDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface arg0) {
                Toast.makeText(getApplicationContext(), "즐겨찾기를 추가하지 않았습니다.", 1000).show();
            }
        });
 
        // 다이얼로그를 띄우는 버튼
        showDialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                favoriteDialog.show();
            }
        });
    }
}


위 코드는 다음과 같이 작동합니다.

즐겨찾기 입력

 

확인 버튼을 클릭했을 때

 

취소 버튼을 클릭했을 때

 


 

Posted by Rcen_J
,
xml로 resource를 생성 example) color.xml


    #FFFFFF
    #000000
    #5D5D5D

위처럼 생성한뒤 drawable에 불러서 사용가능


    
    



Posted by Rcen_J
,

EditText 에서 사용하는 hint 속성 주는 방법입니다. 



- hint Color

 android:textColorHint="#ff000000" 


- hint Size ( hint 사이즈는 텍스트 사이즈를 따라갑니다.)

 android:textSize="12sp" 

출처 : http://muzesong.tistory.com/76

Posted by Rcen_J
,