1. AlertDialog


사용자에게 어떤 내용을 알려주거나 '아니오', '예' 버튼을 선택하도록 만들고 싶을 때 사용된다.

타이틀, 안내 메시지, 아이콘 그리고 예, 아니오 버튼 등을 설정할 수 있다.


알림 대화상자는 AlertDialog 객체를 만들고 show 메소드를 이용해 화면에 표시하게 된다.

AlertDialog 객체를 만들 때는 AlertDialog.Builder 객체를 사용한다.



2. 결과 화면


      



3. 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
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
package com.tistory.qlyh8.pracitice;
 
import android.content.DialogInterface;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    Button button;
    TextView textView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMessage();
            }
        });
    }
 
    public void showMessage(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("안내");
        builder.setMessage("종료하시겠습니까?");
        builder.setIcon(R.drawable.ic_launcher_foreground);
 
        builder.setPositiveButton("예"new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Snackbar.make(textView, "예 버튼이 눌렸습니다.", Snackbar.LENGTH_LONG).show();
            }
        });
 
        builder.setNegativeButton("아니오"new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Snackbar.make(textView, "아니오 버튼이 눌렸습니다.", Snackbar.LENGTH_LONG).show();
            }
        });
 
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
 
cs





출처: https://www.edwith.org/boostcourse-android/lecture/17053/

'Android' 카테고리의 다른 글

Bitmap Button 만들기  (0) 2018.05.01
Nine Patch (나인패치)  (0) 2018.05.01
Snackbar (스낵바)  (0) 2018.04.28
Toast (토스트)  (0) 2018.04.28
Event (이벤트) - 키 이벤트  (0) 2018.04.28

1. Snackbar


아래쪽에서 잠깐 올라왔다가 사라지는 뷰이다.


스낵바를 사용하기 위해서는 build.gradle의 app 수준에서 design dependency를 추가해야 한다.

1
compile 'com.android.support:design:26.1.0'
cs

( File - Project Structure - app - dependencies - + button - library dependency )



2. 결과 화면




3. 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
package com.tistory.qlyh8.pracitice;
 
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends AppCompatActivity {
 
    Button button;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "스낵바입니다.", Snackbar.LENGTH_LONG).show();
            }
        });
    }
}
 
cs





출처: https://www.edwith.org/boostcourse-android/lecture/17052/

'Android' 카테고리의 다른 글

Nine Patch (나인패치)  (0) 2018.05.01
AlertDialog (알림 대화상자)  (0) 2018.04.28
Toast (토스트)  (0) 2018.04.28
Event (이벤트) - 키 이벤트  (0) 2018.04.28
Event (이벤트) - 터치와 제스처 이벤트  (0) 2018.04.28

1. Toast


간단한 메시지를 잠깐 보여주었다가 없어지는 뷰로, 애플리케이션 위에 떠 있는 뷰라 할 수 있다.


Toast.makeText(Context context, String message, int duration)


public void setGravity(int gravity, int xOffset, int yOffset)

public void setMargin(float horizontalMargin, float verticalMargin)



2. 예제1 - 결과 화면




3. 예제1 - 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
package com.tistory.qlyh8.pracitice;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    Button button;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(getApplicationContext(), "위치가 바뀐 토스트", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP|Gravity.LEFT, 100200);
                toast.show();
            }
        });
    }
}
 
cs



4. 예제2- 결과 화면



5. 예제2 - drawable/toast.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
 
    <stroke
        android:color="#295986"
        android:width="10dp"/>
 
    <solid android:color="#eac35f"/>
 
    <padding
        android:left="20dp"
        android:right="20dp"
        android:top="20dp"
        android:bottom="20dp"/>
 
    <corners android:radius="15dp"/>
</shape>
cs



6. 예제2 - toast_layout.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/toast_layout_root">
 
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:textSize="20sp"
        android:background="@drawable/toast"/>
</LinearLayout>
cs



7. 예제2 - MainActivty.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
package com.tistory.qlyh8.pracitice;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    Button button;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)findViewById(R.id.toast_layout_root));
 
                TextView textView = layout.findViewById(R.id.textView);
                textView.setText("모양이 바뀐 토스트");
 
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER, 0-100);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
        });
    }
}
 
cs





출처: https://www.edwith.org/boostcourse-android/lecture/17052/

'Android' 카테고리의 다른 글

AlertDialog (알림 대화상자)  (0) 2018.04.28
Snackbar (스낵바)  (0) 2018.04.28
Event (이벤트) - 키 이벤트  (0) 2018.04.28
Event (이벤트) - 터치와 제스처 이벤트  (0) 2018.04.28
Event (이벤트)  (0) 2018.04.28

+ Recent posts