«   2024/11   »
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
Tags
more
Archives
Today
Total
관리 메뉴

ITGenerations

버튼 yes, no 본문

프로그래밍/안드로이드특강

버튼 yes, no

ITGenerations 2018. 1. 23. 14:00

Java

package com.example.ryan.myapplication;

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

public class MainActivity extends AppCompatActivity {

private static final int DIALOG_YES_NO_MESSAGE=1;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button)findViewById(R.id.button);
}

//버튼 클릭했을 때 호출되는 메소드
public void onButtonClicked(View view){
showDialog(DIALOG_YES_NO_MESSAGE);
}

protected Dialog onCreateDialog(int id){
switch (id){
case DIALOG_YES_NO_MESSAGE:
//객체생성
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//대화상자속성
builder.setTitle("종료학인대화상자")
.setMessage("애플리케이션을종료하시겠습니까")
.setCancelable(false)
//"back"버튼으로 다이러로그를 취소할 수 없다
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.cancel();
//대화상자종료
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
MainActivity.this.finish();
//액티비티종료
}
});
//
AlertDialog alert =builder.create();
return alert;
}
return null;
}
}



xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ryan.myapplication.MainActivity"
tools:layout_editor_absoluteY="81dp">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="22dp"
android:layout_marginTop="30dp"
android:onClick="onButtonClicked"
android:text="대화상자생성" />
</RelativeLayout>