대화상자 (Dialog)


1. AlertDialog (경고창)


모듈은 empty activity로.


1) 레이아웃


<?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: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.progdv.alertdialog.MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Alert Dialog" />

</LinearLayout>


</RelativeLayout>



2) 기능구현


package com.progdv.alertdialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private static final int DIALOG_ID = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 버튼에 리스너 등록 및 이벤트 처리
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// 다이어로그 창을 띄우기
showDialog(DIALOG_ID);
}
});
}

@Override
protected Dialog onCreateDialog(int id) {
// return super.onCreateDialog(id);
AlertDialog.Builder builder = null;
switch (id) {
case 1:
new AlertDialog.Builder(this);
builder.setIcon(R.drawable.warning)
.setTitle("종료 확인 경고창")
.setMessage("어플리케이션을 종료합니다.")
.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish(); // 현재 액티비티 종료
}
})
.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel(); // 대화상자 취소
}
});
}
return builder.create();
}
}





2. ProgressDialog





3. DatePickerDialog






Posted by netyhobby
,