Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 앱 이름 변경
- 한글 깨짐
- 안드로이드
- BFS
- upper_bound
- c언어
- Parametric Search
- 이분 탐색
- scc
- Kosaraju's algorithm
- KMP
- 안드로이드 스튜디오
- 알고리즘
- 계산기
- 최단경로
- C++
- AlertDialog
- lower_bound
- 앱
- 어플
Archives
- Today
- Total
소시지
AlertDialog 본문
다음 사진과 같이 리스트를 만들어 화면에 띄우는 것을 AlertDialog라고 합니다.
XML파일은 버튼만 만들면 되기 때문에 간단합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.geon6.a170122_alertdialog.Activity_main"> <Button android:text="리스트" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:id="@+id/list_button" /> </RelativeLayout> | cs |
이제 Button의 OnClickListemer을 만듭니다.
OnClickListener안에는 다음과 같은 내용을 넣습니다.
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 | package com.example.geon6.a170122_alertdialog; import android.content.DialogInterface; 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.Toast; public class Activity_main extends AppCompatActivity { private String[] items= {"A","B","C"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button list_button = (Button)findViewById(R.id.list_button); list_button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(Activity_main.this); builder.setTitle("선택하세요"); builder.setItems(items, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialogInterface, int i){ Toast toast=Toast.makeText(getApplicationContext(),items[i]+"가 눌러짐",Toast.LENGTH_SHORT); toast.show(); } }); builder.create().show(); } }); } } | cs |
1 | private String[] items= {"A","B","C"}; | cs |
이 String 배열은 AlertDialog에 띄울 목록입니다.
1 | builder.setTitle("선택하세요"); | cs |
1 2 3 4 | public void onClick(DialogInterface dialogInterface, int i){ Toast toast=Toast.makeText(getApplicationContext(),items[i]+"가 눌러짐",Toast.LENGTH_SHORT); toast.show(); } | cs |
이를 수정하면 더 멋진앱을 만들 수 있을 겁니다.
AlertDialog를 사용하면 단순히 목록만 있는 것이 아니라 Yes와 No버튼을 만들어 종료창을 띄울 수도 있습니다.
XML파일에는 id를 exitButton로 가지는 버튼을 하나 만들어줍니다.
그리고 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 | package com.example.geon6.a170205_alertdialog_exit; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Activity_main extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button)findViewById(R.id.btn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder=new AlertDialog.Builder(Activity_main.this); builder.setMessage("정말로 종료하시겠습니까?"); builder.setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); AlertDialog alert=builder.create(); alert.setTitle("종료 알림창"); alert.show(); } }); } } | cs |
다음을 안드로이드에서 실행시키면 종료알림창이 띄어집니다.
'Android' 카테고리의 다른 글
앱 이름 바꾸기 (0) | 2017.01.23 |
---|---|
계산기 앱(어플) 만들기 (0) | 2017.01.23 |
안드로이드 스튜디오 한글깨짐 현상 고치기 (0) | 2017.01.22 |
Hello World! (0) | 2017.01.16 |
Comments