1.提示對話框
- 界面
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提示對話框"
android:onClick="onDialogClick1"/>
- 主方法
public void onDialogClick1(View v){//使用提示對話框實現退出程序的提示
new AlertDialog.Builder(MainActivity.this)//設置相關屬性,需要在當前活動中
.setIcon(android.R.drawable.alert_dark_frame)//設置圖標
.setTitle("注意!!!" )//設置標題
.setMessage("確定要退出嗎???")//設置內容
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();//關閉當前活動
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create().show();
}
2.單選對話框
- 界面
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="單選對話框"
android:onClick="onDialogClick2"/>
- 主方法
public void onDialogClick2(View v){
final String[] items={"男","女","F","M"};//準備數據源
AlertDialog dialog=new AlertDialog.Builder(this)
.setTitle("單選對話框")
.setIcon(R.drawable.cat_small)
.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
})
.create();
dialog.show();
}
3.複選對話框
- 界面
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="複選對話框"
android:onClick="onDialogClick3"/>
- 主方法
public void onDialogClick3(View v){
final String[] items3={"北京","上海","廣州","深圳","天津","保定"};
AlertDialog dialog=new AlertDialog.Builder(this)
.setTitle("多選對話框")
.setIcon(R.drawable.cat_small)
.setPositiveButton("確定",null)
.setNegativeButton("取消",null)
.setMultiChoiceItems(items3, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
String itemStr=items3[which];
Toast.makeText(MainActivity.this,"選擇了"+itemStr,Toast.LENGTH_SHORT).show();
}
})
.create();
dialog.show();
}
4.列表對話框
列表對話框
- 界面
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表對話框"
android:onClick="onDialogClick4"/>
- 主方法
public void onDialogClick4(View v){
final String[] items4={"北京","上海","廣州","深圳","天津","保定"};
AlertDialog dialog=new AlertDialog.Builder(this)
.setTitle("列表對話框")
.setIcon(R.drawable.cat_small)
.setItems(items4, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,items4[which],Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
})
.create();
dialog.show();
}
5.進度條對話框
- 界面
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="進度條對話框"
android:onClick="onDialogClick5"/>
- 主方法
public void onDialogClick5(View v){
ProgressDialog pd1=ProgressDialog.show(this,"提示","正在登錄中",false,true);
}
6.日期選擇對話框
- 界面
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="日期選擇對話框"
android:onClick="onDialogClick6"/>
- 主方法
public void onDialogClick6(View v){
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());//讀出當前時間
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH);
final int day=calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {//當時間被設置後回調的方法
Toast.makeText(MainActivity.this,year+"年"+month+"月"+day+"日",Toast.LENGTH_SHORT).show();
}
},year,month,day);
dpd.show();
}
一般的對話框都要顯式調用dismiss()方法來回收對話框,在DatePickerDialog對話框中,自帶的兩個按鍵的監聽中已經默認調用dissmiss()了。
7.時間選擇對話框
- 界面
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="時間選擇對話框"
android:onClick="onDialogClick7"/>
- 主方法
public void onDialogClick7(View v){
Calendar calendar=Calendar.getInstance();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this,hourOfDay+":"+minute+"!",Toast.LENGTH_SHORT).show();
}
},
calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();
}
8.拖動對話框
使用步驟如下:
(1)準備一個簡單的佈局文件,裏面設置SeekBar控件;
(2)直接創建Dialog實例對象(Dialog dialog = new Dialog(…));
(3)利用對話框實例將佈局文件設置到對話框中(dialog.setContentView(…));
(4)顯示對話框dialog .show();
(5)通過對話框實例找到設置進去的控件(SeekBar sbar = dialog .findViewById(seekBar的id));
(6)進行響應的監聽sBar.setOnSeekBarChangeListener()。
- 界面
<Button
android:id="@+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拖動對話框"
android:onClick="onDialogClick8"/>
- Seek.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below = "@+id/seekBar1"
android:layout_centerHorizontal = "true"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="100dip"
android:id="@+id/seekBar1"/>
</RelativeLayout>
- 主方法
public void onDialogClick8(View v){
Dialog dialog=new Dialog(this);
dialog.setTitle("拖動對話框");
dialog.setContentView(R.layout.seek);
SeekBar sbar=(SeekBar)dialog.findViewById(R.id.seekBar1);
sbar.setMax(100);
final TextView tview=(TextView)dialog.findViewById(R.id.tv_result);
tview.setText("當前進度爲:"+sbar.getProgress());
sbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tview.setText("設置音量大小爲:"+seekBar.getProgress());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
dialog.show();
}