반응형

출처: http://4eda.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-%EB%B0%B0%EA%B2%BD%ED%99%94%EB%A9%B4-%EB%B2%84%ED%8A%BC-%EC%9D%B4%EB%AF%B8%EC%A7%80-%EB%B3%80%EA%B2%BD-LinearLayout

 

기본화면입니다.

 


버튼설정은 앞선강의(액티비티간 전환)를 참조하세요.

 

1. 버튼이미지 삽입

 


<Button
 android:id="@+id/bgchange1"
 android:layout_width="fill_parent"
 android:layout_height="40px"
 android:text="@string/str_bg1"
 android:background="@color/red"
 />


 

1) main.xml에서 버튼의 배경을 설정합니다.




2) res -> values -> colors.xml파일을 생성합니다. 

 


 

<?xml version="1.0" encoding="UTF-8"?>
< resources>
  <color name="red">#ffff0000</color>
  <color name="blue">#ff0000ff</color>
< /resources>



2. 배경화면이미지 삽입

 


1) main.xml 파일에 LinearLayout id를 정의합니다.

 


2) 원하는 이미지 파일을 드래그하여 drawable-hdpi 폴더로 옮겨줍니다.

 

위와 같은 창이 뜨면 OK를 선택합니다.

 


폴더 안에 이미지파일이 들어간 것을 볼 수 있습니다.

 


 

public class Layout extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    Button button1;
    Button button2;
    LinearLayout layout;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        button1 = (Button)findViewById(R.id.bgchange1);
        button2 = (Button)findViewById(R.id.bgchange2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        layout=(LinearLayout)findViewById(R.id.layout);
    }
 @Override
 public void onClick(View v){
  if(v.getId()==R.id.bgchange1){
   layout.setBackgroundResource(R.drawable.rose);
  }
  else if(v.getId()==R.id.bgchange2){
   layout.setBackgroundResource(R.drawable.cloud);
  }
 }
}


3) Layout.java 파일을 작성합니다.

Main.xml화면에 정의된 layout를 사용하여 버튼이벤트로 저장된 이미지로 배경화면이 변환되도록 합니다.


3. 결과

 

 


 

버튼을 클릭할 때마다 배경화면이 변하는 것을 확인할 수 있습니다.

반응형
반응형

출처: http://arabiannight.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9CAndroid-%EA%B8%B0%EB%B3%B8-Dialog-inflate-Dialog

 

AlertDialog를 사용한 기본 Dialog 예제 입니다.


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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package arabiannight.tistory.com.simpledialog;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class TestSimpleDialogActivity extends Activity implements OnClickListener {
     
    private AlertDialog mDialog = null;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        setLayout();
         
    }
     
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_base:
            mDialog = createDialog();
            mDialog.show();
            break;
             
        case R.id.btn_inflate:
            mDialog = createInflaterDialog();
            mDialog.show();
            break
 
        default:
            break;
        }
    }
     
     
    /**
     * base 다이얼로그
     * @return ab
     */
    private AlertDialog createDialog() {
        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setTitle("Title");
        ab.setMessage("내용");
        ab.setCancelable(false);
        ab.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
          
        ab.setPositiveButton("확인", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                setDismiss(mDialog);
            }
        });
          
        ab.setNegativeButton("취소", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                setDismiss(mDialog);
            }
        });
          
        return ab.create();
    }
     
     
    /**
     * Infalter 다이얼로그
     * @return ab
     */
    private AlertDialog createInflaterDialog() {
        final View innerView = getLayoutInflater().inflate(R.layout.dialog_layout, null);
        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setTitle("Title");
        ab.setView(innerView);
          
        ab.setPositiveButton("확인", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                setDismiss(mDialog);
            }
        });
          
        ab.setNegativeButton("취소", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                setDismiss(mDialog);
            }
        });
          
        return ab.create();
    }
     
    /**
     * 다이얼로그 종료
     * @param dialog
     */
    private void setDismiss(Dialog dialog){
        if(dialog != null && dialog.isShowing())
            dialog.dismiss();
    }
 
 
    /*
     * Layout
     */
    private Button baseButton = null;
    private Button inflateButton = null;
     
    private void setLayout(){
        baseButton = (Button) findViewById(R.id.btn_base);
        inflateButton = (Button) findViewById(R.id.btn_inflate);
         
        baseButton.setOnClickListener(this);
        inflateButton.setOnClickListener(this);
    }
}





파일첨부 :


TestSimpleDialog.zip


스크린샷 : 


반응형
반응형

출처: http://egloos.zum.com/surprisen/v/2415659

 

<다이얼로그가 뜨는 소스>

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,(ViewGroup) findViewById(R.id.layout_root));
AlertDialog.Builder aDialog = new AlertDialog.Builder(mtsGetLatLng.this);
aDialog.setTitle("로그인하시겠습니까?");
aDialog.setView(layout);
aDialog.setPositiveButton("로그인", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
aDialog.setNegativeButton("취소", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog ad = aDialog.create();
ad.show();

<custom_dialog.xml 파일의 소스>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dp">
<TextView android:text="ID" android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/image" android:layout_width="200dip"
android:layout_height="35dip" android:layout_marginRight="10dp" />
<TextView android:text="Password" android:id="@+id/TextView02"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/text" android:layout_width="200dip"
android:layout_height="35dip" android:textColor="#FFF" />
</LinearLayout>
반응형
반응형

출처: http://growingdever.tistory.com/99

 

안드로이드에서는 다이얼로그를 AlertDialog라고 합니다.
개인적인 이유로 텍스트를 입력할 수 있는 다이얼로그를 만들어야 하는 상황이 왔는데
대부분 리스트가 있거나 라디오버튼이 있는 등의 다이얼로그를 만드는 방법만 소개되어 있더군요.
힘들게 찾아냈는데 다른 분들도 볼 수 있게 공유하려 합니다.

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Title");
alert.setMessage("Message");

// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
value.toString();
// Do something with value!
}
});

alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});

alert.show(); 

이렇게 하면 타이틀이 Title, 다이얼로그 내용이 Message, 텍스트를 입력 받을 수 있는 form, OK 버튼과 Cancel 버튼이 있는
AlertDialog가 화면에 나타나게 됩니다. 

반응형

+ Recent posts