IT/Android

android - AlertDialog, setItems, setSingleChoiceItems, setMultiChoiceItems

노마드오브 2018. 9. 21. 22:01
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center_horizontal"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="대화상자" />

</LinearLayout>


package com.example.it.myapplication7_7;

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 MainActivity extends AppCompatActivity {

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

// final Button button1 = (Button) findViewById(R.id.button1);
// button1.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// final String[] versionArray = new String[] {"진저브레드", "허니콤", "아이스크림"};
// AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
// dlg.setTitle("좋아하는 버전은?");
// dlg.setIcon(R.drawable.ic_launcher_background);
// dlg.setItems(versionArray, new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// button1.setText(versionArray[which]);
// }
// });
// dlg.setPositiveButton("닫기", null);
// dlg.show();
// }
// });

// final Button button1 = (Button) findViewById(R.id.button1);
// button1.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// final String[] versionArray = new String[] {"진저브레드", "허니콤", "아이스크림"};
// AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
// dlg.setTitle("좋아하는 버전은?");
// dlg.setIcon(R.drawable.ic_launcher_background);
// dlg.setSingleChoiceItems(versionArray, 0, new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// button1.setText(versionArray[which]);
// }
// });
// dlg.setPositiveButton("닫기", null);
// dlg.show();
// }
// });

final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String[] versionArray = new String[] {"진저브레드", "허니콤", "아이스크림"};

final boolean[] checkArray = new boolean[] {true, false, false};
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("좋아하는 버전은?");
dlg.setIcon(R.drawable.ic_launcher_background);
dlg.setMultiChoiceItems(versionArray, checkArray, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
button1.setText(versionArray[which]);
}
});
dlg.setPositiveButton("닫기", null);
dlg.show();
}
});

}
}