AndroidにおけるDialog用法大全(1)
Androidでのダイアログ効果の応用.一般的なダイアログボックスの使用、ポップアップダイアログボックス、日付選択ダイアログボックス、時間選択ダイアログボックス、進捗バーダイアログボックス.この資料はDialogの使い方をもっと理解します.コードを見てみましょう.
ここでは6つのbuttonを定義します.これらのButtonは役に立ちます.各Buttonはどのような意味を表していますか.次のコードを見てみましょう.
<?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/txtMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
ここでは6つのbuttonを定義します.これらのButtonは役に立ちます.各Buttonはどのような意味を表していますか.次のコードを見てみましょう.
package eoe.android.dialog;
import java.util.Calendar;
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.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Demo。 Builder(), Create(), show()
AlertDialog dialog = new AlertDialog.Builder(this).setTitle(" ").create();
dialog.show();
// Demo
MyButtonClickListener listener = new MyButtonClickListener();
Button btn1 = (Button) this.findViewById(R.id.btn1);
btn1.setText(" Demo");
btn1.setOnClickListener(listener);
Button btn2 = (Button) this.findViewById(R.id.btn2);
btn2.setText(" ( xml)");
btn2.setOnClickListener(listener);
Button btn3 = (Button) this.findViewById(R.id.btn3);
btn3.setText(" View");
btn3.setOnClickListener(listener);
Button btn4 = (Button) this.findViewById(R.id.btn4);
btn4.setText(" ");
btn4.setOnClickListener(listener);
Button btn5 = (Button) this.findViewById(R.id.btn5);
btn5.setText(" ");
btn5.setOnClickListener(listener);
Button btn6 = (Button) this.findViewById(R.id.btn6);
btn6.setText(" ");
btn6.setOnClickListener(listener);
}
class MyButtonClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// onCreateDialog
switch (v.getId()) {
case R.id.btn1:
Main.this.showDialog(0);
break;
case R.id.btn2:
Main.this.showDialog(1);
break;
case R.id.btn3:
Main.this.showDialog(2);
break;
case R.id.btn4:
Main.this.showDialog(3);
break;
case R.id.btn5:
Main.this.showDialog(4);
break;
case R.id.btn6:
Main.this.showDialog(5);
break;
}
}
}
@Override
public Dialog onCreateDialog(int id) {
switch (id) {
case 0:
//
return new AlertDialog.Builder(this).setTitle(" Demo")
.create();
case 1:
//
return new AlertDialog.Builder(this)
.setTitle(" ") //
// .setCustomTitle(View) // View
.setIcon(R.drawable.icon01) //
// .setMessage(" ") //
.setPositiveButton(" ", new OnClickListener() { // ,
@Override
public void onClick(DialogInterface a0, int a1) {
TextView txtMsg = (TextView) Main.this.findViewById(R.id.txtMsg);
txtMsg.append(" “ ”
");
}
})
.setItems(R.array.ary, new DialogInterface.OnClickListener() { // 。
public void onClick(DialogInterface dialog, int which) {
}
})
//
// .setMultiChoiceItems(arg0, arg1, arg2)
// .setSingleChoiceItems(arg0, arg1, arg2)
// .setNeutralButton(arg0, arg1)
// .setNegativeButton(arg0, arg1)
.create();
case 2:
// View Demo
return new AlertDialog.Builder(this).setTitle(" View")
.setView(this.findViewById(R.layout.view)).create();
case 3:
//
Calendar c = Calendar.getInstance();
return new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
TextView txtMsg = (TextView) Main.this.findViewById(R.id.txtMsg);
txtMsg.append(" :" + String.valueOf(year) + "-"
+ String.valueOf(monthOfYear) + "-"
+ String.valueOf(dayOfMonth) + "
");
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
case 4:
//
Calendar c2 = Calendar.getInstance();
return new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
TextView txtMsg = (TextView) Main.this.findViewById(R.id.txtMsg);
txtMsg.append(" :"
+ String.valueOf(hourOfDay) + ":"
+ String.valueOf(minute) + "
");
}
}, c2.get(Calendar.HOUR), c2.get(Calendar.MINUTE), true);
case 5:
//
ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("loading...");
return progress;
default:
return null;
}
}
}