2011/03/21 13:33


13-4 대화상자

[01] 대화 상자

1. 단순한 대화 상자
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id="@+id/text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="아래 버튼을 눌러 대화상자를 호출하시오."
    />
<Button
    android:id="@+id/call" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="대화상자 호출"
    />
</LinearLayout>

 

 

package exam.dialog;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import exam.AndroidExam.*;

public class DialogTest extends Activity {
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dialog_dialogtest);

  Button btn = (Button)findViewById(R.id.call);
  btn.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    //* 빌더 생성 후 속성 설정
    AlertDialog.Builder bld = new AlertDialog.Builder(DialogTest.this);
    bld.setTitle("알립니다.");
    bld.setMessage("대화상자를 열었습니다.");
    bld.setIcon(R.drawable.icon);
    bld.show();
    //*/

    /* 연쇄적인 호출
    new AlertDialog.Builder(DialogTest.this)
    .setTitle("알립니다.")
    .setMessage("대화상자를 열었습니다.")
    .setIcon(R.drawable.icon)
    .show();
    //*/
   }
  });  
 }
}

 


2. 대화상자의 버튼 생성

package exam.dialog;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import exam.AndroidExam.*;

public class DialogButton extends Activity {
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dialog_dialogtest);

  Button btn = (Button)findViewById(R.id.call);
  btn.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    /* 빈 리스너 작성
       new AlertDialog.Builder(DialogButton.this)
       .setTitle("알립니다.")
       .setMessage("대화상자를 열었습니다.")
       .setIcon(R.drawable.icon)
       .setPositiveButton("닫기", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
     }
    })
       .show();
       //*/

    /* 부정 버튼 배치
       new AlertDialog.Builder(DialogButton.this)
       .setTitle("알립니다.")
       .setMessage("대화상자를 열었습니다.")
       .setIcon(R.drawable.icon)
       .setNegativeButton("닫기", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
     }
    })
       .show();
       //*/

       //* null 리스너 작성
       new AlertDialog.Builder(DialogButton.this)
       .setTitle("알립니다.")
       .setMessage("대화상자를 열었습니다.")
       .setIcon(R.drawable.icon)
       .setPositiveButton("닫기", null)
       .show();
       //*/
   }
  });  
 }
}

 


3. 동적 대화상자의 생성

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id="@+id/text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="아래 버튼을 눌러 대화상자를 호출하시오."
    />
<Button
    android:id="@+id/call" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="알림 대화상자"
    />
<Button
    android:id="@+id/call2" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="질문 대화상자"
    />
</LinearLayout>

 

package exam.dialog;

import java.util.*;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import exam.AndroidExam.*;

public class ShowDialog extends Activity {
 final static int SampleDialog = 0;
 final static int QuestionDialog = 1;
 
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dialog_showdialog);

  Button btn = (Button)findViewById(R.id.call);
  btn.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    showDialog(SampleDialog);
   }
  });  
  Button btn2 = (Button)findViewById(R.id.call2);
  btn2.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    showDialog(QuestionDialog);
   }
  });  
 }
 
    protected Dialog onCreateDialog(int id) {
     switch (id) {
     case SampleDialog:
      return new AlertDialog.Builder(ShowDialog.this)
   .setTitle("알립니다.")
   .setMessage("대화상자를 열었습니다.")
   .setIcon(R.drawable.icon)
   .setPositiveButton("닫기", null)
   .create();
     case QuestionDialog:
      return new AlertDialog.Builder(ShowDialog.this)
   .setTitle("질문")
   .setMessage("밥 먹었어요?")
   .setPositiveButton("먹었다", null)
   .setNegativeButton("굶었다", null)
   .create();
     }
     return null;
    }

    protected void onPrepareDialog(int id, Dialog dialog) {
     super.onPrepareDialog(id, dialog);
     switch (id) {
     case SampleDialog:
      break;
     case QuestionDialog:
      Calendar calendar = Calendar.getInstance();
      String stime = String.format("%d시 %d분 %d초",
        calendar.get(Calendar.HOUR_OF_DAY),
        calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
      dialog.setTitle(stime);
      break;
     }
    }
}

 


4. 목록 선택 대화 상자
>>>>> res/values/arrys.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="country">
        <item>신라</item>
        <item>고구려</item>
        <item>백제</item>
        <item>당나라</item>
        <item>발해</item>
        <item>옥저</item>
        <item>동예</item>
        <item>부여</item>
        <item>대한민국</item>
    </string-array>
    <string-array name="fruits">
        <item>사과</item>
        <item>딸기</item>
        <item>수박</item>
        <item>바나나</item>
        <item>감</item>
        <item>복숭아</item>
        <item>자두</item>
    </string-array>
    <string-array name="foods">
        <item>짜장면</item>
        <item>우동</item>
        <item>짬뽕</item>
        <item>탕수육</item>
    </string-array>
</resources>

 


package exam.dialog;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import exam.AndroidExam.*;

public class SelectDialog1 extends Activity {
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dialog_dialogtest);

  Button btn = (Button)findViewById(R.id.call);
  btn.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    new AlertDialog.Builder(SelectDialog1.this)
    .setTitle("음식을 선택하시오.")
    .setIcon(R.drawable.icon)
    .setItems(R.array.foods,
    // .setItems(new String[] {"짜장면", "우동", "짱뽕", "탕수육" },
     new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int which) {
      String[] foods = getResources().getStringArray(R.array.foods);
      TextView text = (TextView)findViewById(R.id.text);
      text.setText("선택한 음식 = " + foods[which]);
     }
    })
    .setNegativeButton("취소", null)
    .show();
   }
  });  
 }
}

 

 

5. 확인, 취소 버튼이 있는 목록 선택 대화 상자
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id="@+id/text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="아래 버튼을 눌러 대화상자를 호출하시오."
    />
<Button
    android:id="@+id/call" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="대화상자 호출"
    />
</LinearLayout>

 


package exam.dialog;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import exam.AndroidExam.*;

public class SelectDialog2 extends Activity {
 int mSelect = 0;
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dialog_dialogtest);

  Button btn = (Button)findViewById(R.id.call);
  btn.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    new AlertDialog.Builder(SelectDialog2.this)
    .setTitle("음식을 선택하시오.")
    .setIcon(R.drawable.icon)
    .setSingleChoiceItems(R.array.foods, mSelect,
      new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int which) {
      mSelect = which;
     }
    })
    .setPositiveButton("확인", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
      String[] foods = getResources().getStringArray(R.array.foods);
      TextView text = (TextView)findViewById(R.id.text);
      text.setText("선택한 음식 = " + foods[mSelect]);
     }
    })
    .setNegativeButton("취소", null)
    .show();
   }
  });  
 }
}

 

 

6. 체크 상자가 있는 대화 상자
package exam.dialog;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import exam.AndroidExam.*;

public class SelectDialog3 extends Activity {
 boolean[] mSelect = { false, false, false, false };
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dialog_dialogtest);

  Button btn = (Button)findViewById(R.id.call);
  btn.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    new AlertDialog.Builder(SelectDialog3.this)
    .setTitle("음식을 선택하시오.")
    .setIcon(R.drawable.icon)
    .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.text);
      String result = "선택한 음식 = ";
      for (int i = 0; i < mSelect.length; i++) {
       if (mSelect[i]) {
        result += foods[i] + " ";
       }
      }
      text.setText(result);
     }
    })
    .setNegativeButton("취소", null)
    .show();
   }
  });  
 }
}

 


7.커스텀 대화 상자
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5pt"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="제품"
    />
<EditText
    android:id="@+id/product" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="수량"
    />
<EditText
    android:id="@+id/number" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:numeric="integer"
    />
<CheckBox
    android:id="@+id/paymethod"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="착불 결제"
    />
</LinearLayout>

 


package exam.dialog;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import exam.AndroidExam.*;

public class OrderDialog extends Activity {
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dialog_dialogtest);

  Button btn = (Button)findViewById(R.id.call);
  btn.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    final LinearLayout linear = (LinearLayout)
     View.inflate(OrderDialog.this, R.layout.dialog_order, null);
    
    new AlertDialog.Builder(OrderDialog.this)
    .setTitle("주문 정보를 입력하시오.")
    .setIcon(R.drawable.icon)
    .setView(linear)
    .setPositiveButton("확인", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
      EditText product = (EditText)linear.findViewById(R.id.product);
      EditText number = (EditText)linear.findViewById(R.id.number);
      CheckBox paymethod = (CheckBox)linear.findViewById(R.id.paymethod);
      TextView text = (TextView)findViewById(R.id.text);
      text.setText("주문 정보 " + product.getText() + " 상품 " +
        number.getText() + "개." +
        (paymethod.isChecked() ? "착불결제":""));
     }
    })
    .setNegativeButton("취소", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
      TextView text = (TextView)findViewById(R.id.text);
      text.setText("주문을 취소했습니다.");
     }
    })
    .show();
   }
  });  
 }
}

 

 

 

 

Posted by 權珉旭

티스토리 툴바