2009年7月12日日曜日

Android - Dialog を使う -

Dialog





AlertDialog






DatePickerDialog






TimePickerDialog






ProgressDialog

progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);



progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);





More...



res/layout/dialog_layout.xml


android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dialog_question"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:padding="30dip"
>
android:id="@+id/dialog_coffee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dialog_coffee_label"
android:layout_weight="1"
/>
android:id="@+id/dialog_tea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dialog_tea_label"
android:layout_weight="1"
/>







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.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class DialogTest extends Activity {

EditText editText;

Dialog dialog;
AlertDialog alertDialog;
DatePickerDialog datePickerDialog;
TimePickerDialog timePickerDialog;
ProgressDialog progressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

editText = (EditText) findViewById(R.id.text);

setDialog();
setAlertDialog();
setDatePickerDialog();
setTimePickerDialog();
setProgressDialog();
}

// Dialog
public void setDialog(){
dialog = new Dialog(DialogTest.this);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle("飲み物は?");
dialog.findViewById(R.id.dialog_coffee).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
}
);
dialog.findViewById(R.id.dialog_tea).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
}
);

dialogButton = (Button) findViewById(R.id.dialog_test);
dialogButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
dialog.show();
}
}
);
}

// AlertDialog
public void setAlertDialog(){
alertDialog = new AlertDialog.Builder(DialogTest.this)
.setTitle("給食")
.setMessage("カレーライスは好きですか?")
.setIcon(R.drawable.icon)
.setPositiveButton("はい", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di, int whichButton) {
}
})
.setNegativeButton("いいえ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di, int whichButton) {
}
})
.setNeutralButton("どちらでもない", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di, int whichButton) { }
})
.create();

findViewById(R.id.alertDialog_test).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
alertDialog.show();
}
});
}

// DatePickerDialog
public void setDatePickerDialog(){

DatePickerDialog.OnDateSetListener listener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker pikcer, int year, int month, int day) {
editText.setText(year + "年" + month + "月" + day + "日");
}
};

datePickerDialog = new DatePickerDialog(DialogTest.this, listener, 2009, 7, 12);

findViewById(R.id.datePickerDialog_test).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
datePickerDialog.show();
}
});
}

// TimePickerDialog
public void setTimePickerDialog() {

TimePickerDialog.OnTimeSetListener listener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker picker, int hour, int min) {
editText.setText(String.format("%02d時 %02d分", hour, min));
}
};

timePickerDialog = new TimePickerDialog(DialogTest.this, listener, 0, 0, true);

findViewById(R.id.timePickerDialog_test).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v){
timePickerDialog.show();
}
});
}

// ProgressDialog
public void setProgressDialog() {

progressDialog = new ProgressDialog(this);
progressDialog.setTitle("調理中");
progressDialog.setMessage("カレーライスを作ってます~");
progressDialog.setIndeterminate(false);
//progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMax(10);
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface di) {

}
});

findViewById(R.id.progressDialog_test).setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
progressDialog.show();

new Thread(new Runnable() {
public void run() {
try {
for (int i=0; i < progressDialog.getMax(); i++) {
progressDialog.setProgress(i);
Thread.sleep(1000);
}
}catch (InterruptedException e){
}
progressDialog.dismiss();
}
}).start();
}
});
}

0 件のコメント:

コメントを投稿