본문 바로가기

Android

대화상자 - MultiChoiceItems

activity_main.xml

 

<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=".MainActivity" >

 

    <Button

        android:id="@+id/btn"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="다이얼 로그 보이기" />

 

</RelativeLayout>

 

 

MainActivity.java

 

package com.example.dialogex02;

 

import android.os.Bundle;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.view.Menu;

import android.view.View;

import android.widget.TextView;

 

public class MainActivity extends Activity {

     boolean[] mSelect= { false, false, false, false };

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          

           findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {

               

                @Override

                public void onClick(View v) {

                     new AlertDialog.Builder(MainActivity.this)

                     .setTitle("음식을선택하시오.")

                     .setIcon(R.drawable.ic_launcher)

                     .setMultiChoiceItems(R.array.foods, mSelect,

                     new DialogInterface.OnMultiChoiceClickListener() {

        public void onClick( DialogInterface dialog, int which, boolean isChecked) {

                     mSelect[which] = isChecked;

                     }

                     })

                     .setPositiveButton("확인", new DialogInterface.OnClickListener() {

                     public void onClick(DialogInterface dialog, int whichButton) {

                     String[] foods = getResources().getStringArray(R.array.foods);

                     TextView text = (TextView)findViewById(R.id.btn);

                     String result = "선택한음식= ";

                     for (int i = 0; i < mSelect.length; i++) {

                     if (mSelect[i]) {

                     result += foods[i] + " ";

                     }

                     }

                     text.setText(result);

                     }

                     })

                     .setNegativeButton("취소", null)

                     .show();

                    

                }

           });

     }

 




     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}

 

'Android' 카테고리의 다른 글

액티비티(activity) -  (0) 2013.04.03
대화상자 - 커스텀대화상자  (0) 2013.04.02
대화상자 - SingleChoiceItems  (0) 2013.04.02
대화상자 - SelectDialog  (0) 2013.04.02
대화상자 - AlertDialog  (0) 2013.04.02