// .java
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
// Dialog 호출을 위한 상수 정의
static final int DIALOG_SIMPLE1 = 0;
static final int DIALOG_SIMPLE2 = 1;
static final int DIALOG_LIST = 3;
static final int DIALOG_CHECKBOX = 4;
static final int DIALOG_RADIO = 5;
static final int DIALOG_PROGRESS1 = 6;
static final int DIALOG_PROGRESS2 = 7;
static final int DIALOG_DATEPICKER = 8;
static final int DIALOG_TIMEPICKER = 9;
public static final int MENU_INFO = Menu.FIRST;
Button simpleAlertDialog1;
Button simpleAlertDialog2;
Button listAlertDialog;
Button checkboxAlertDialog;
Button radioAlertDialog;
Button progressDialog1;
Button progressDialog2;
ProgressDialog progressDialog;
// DatePickerDialog
private TextView mDateDisplay;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
// TimePickerDialog
private TextView mTimeDisplay;
private Button mPickTime;
private int mHour;
private int mMinute;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int total = msg.getData().getInt("total");
progressDialog.setProgress(total);
if (total >= 100) {
progressDialog.dismiss();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
simpleAlertDialog1 = (Button) findViewById(R.id.simpleAlertDialog1);
simpleAlertDialog1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_SIMPLE1);
}
});
simpleAlertDialog2 = (Button) findViewById(R.id.simpleAlertDialog2);
simpleAlertDialog2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showSimpleDialog();
}
});
listAlertDialog = (Button) findViewById(R.id.listAlertDialog);
listAlertDialog.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_LIST);
}
});
checkboxAlertDialog = (Button) findViewById(R.id.checkboxAlertDialog);
checkboxAlertDialog.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_CHECKBOX);
}
});
radioAlertDialog = (Button) findViewById(R.id.radioAlertDialog);
radioAlertDialog.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_RADIO);
}
});
progressDialog1 = (Button) findViewById(R.id.progressDialog1);
progressDialog1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_PROGRESS1);
TimerTask myTask = new TimerTask() {
public void run() {
dismissDialog(DIALOG_PROGRESS1);
}
};
Timer timer = new Timer();
timer.schedule(myTask, 5000);
}
});
progressDialog2 = (Button) findViewById(R.id.progressDialog2);
progressDialog2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_PROGRESS2);
new Thread(new Runnable() {
public void run() {
try {
for (int i = 0; i <= 100; i++) {
Thread.sleep(100);
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("total", i);
msg.setData(b);
handler.sendMessage(msg);
}
} catch (Throwable t) {
}
}
}).start();
}
});
// DatePicker Dialog Test
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
mPickDate = (Button) findViewById(R.id.pickDate);
mPickDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_DATEPICKER);
}
});
// 현재 날짜를 가져온다.
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date (this method is below)
updateDateDisplay();
// TimePicker Dialog Test
mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
mPickTime = (Button) findViewById(R.id.pickTime);
mPickTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_TIMEPICKER);
}
});
final Calendar c1 = Calendar.getInstance();
mHour = c1.get(Calendar.HOUR_OF_DAY);
mMinute = c1.get(Calendar.MINUTE);
updateTimeDisplay();
}
protected Dialog onCreateDialog(int id) {
Dialog dialog;
AlertDialog.Builder builder;
switch (id) {
case DIALOG_SIMPLE1:
builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
MainActivity.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
case DIALOG_LIST:
// array xml에서 불러옴.
final String[] colors = getResources().getStringArray(
R.array.colors);
builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(colors, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), colors[item],
Toast.LENGTH_SHORT).show();
}
});
dialog = builder.create();
break;
case DIALOG_CHECKBOX:
final CharSequence[] items = { "Red", "Green", "Blue" };
final boolean[] states = { false, false, true };
builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
// setMultiChoiceItems(int itemsId, boolean[] checkedItems,
// DialogInterface.OnMultiChoiceClickListener listener)
builder.setMultiChoiceItems(items, states,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int item,
boolean state) {
Toast.makeText(getApplicationContext(),
items[item] + " set to " + state,
Toast.LENGTH_SHORT).show();
// 자동으로 닫기지는 않는다.
}
});
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String msg = "";
for (boolean state : states) {
msg += String.valueOf(state) + " ";
}
Toast.makeText(getApplicationContext(), msg,
Toast.LENGTH_LONG).show();
}
});
dialog = builder.create();
break;
case DIALOG_RADIO:
final CharSequence[] items1 = { "Red", "Green", "Blue" };
builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
// setSingleChoiceItems(CharSequence[] items, int checkedItem,
// DialogInterface.OnClickListener listener)
builder.setSingleChoiceItems(items1, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(),
items1[item], Toast.LENGTH_SHORT).show();
// 자동으로 닫기지 않는다.
dialog.dismiss();
}
});
dialog = builder.create();
break;
case DIALOG_PROGRESS1:
dialog = ProgressDialog.show(MainActivity.this, "",
"Loading, Please Wait..", true, true);
break;
case DIALOG_PROGRESS2:
dialog = new ProgressDialog(MainActivity.this);
((ProgressDialog) dialog)
.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
((ProgressDialog) dialog).setMessage("Loading..");
((ProgressDialog) dialog).setCancelable(true);
progressDialog = (ProgressDialog) dialog;
break;
case DIALOG_DATEPICKER:
dialog = new DatePickerDialog(MainActivity.this, mDateSetListener,
mYear, mMonth, mDay);
break;
case DIALOG_TIMEPICKER:
dialog = new TimePickerDialog(MainActivity.this, mTimeSetListener,
mHour, mMinute, true);
break;
default:
dialog = null;
}
return dialog;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDateDisplay();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateTimeDisplay();
}
};
private void showSimpleDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
}
private void updateDateDisplay() {
mDateDisplay.setText(new StringBuilder().append(mYear).append("-")
.append(mMonth + 1).append("-").append(mDay));
}
private void updateTimeDisplay() {
mTimeDisplay.setText(new StringBuilder().append(pad(mHour)).append(":")
.append(pad(mMinute)));
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MainActivity.MENU_INFO, 1,
getResources().getText(R.string.menu_info)).setIcon(
R.drawable.ic_menu_notifications);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MainActivity.MENU_INFO:
Toast.makeText(this, "info menu is clicked", Toast.LENGTH_SHORT)
.show();
return (true);
}
return super.onOptionsItemSelected(item);
}
}
// .xml
RECENT COMMENT