对话框 Dialog
用户用于做出决定 ,或者添加额外信息 不填充屏幕 只有被触发才执行
经常使用的对话框
- 普通的对话框 AlertDialog
- AlertDialog.Builder
- 带进度条的对话框 ProgressDialog
- ProgressDialog a = new ProgressDialog(参数);
- 日期选择对话框 DatePickerDialog
- DatePickerDialog dialog = new DatePickerDialog()
- 时间选择对话框 TimePickerDialog
- TimePickerDialog dialog = new TimePickerDialog(参数)
- 自定义的对话框
- AlertDialog.Builder buidler = new AlertDialog.Builder(this); buidler.setView(view);
- 普通的对话框 AlertDialog
其他的对话框:
- 普通列表对话框
- AlertDialog.Builder
- setSingleChoiceItems 和单元不同的是没有小框勾选
- 单选列表对话框
- AlertDialog.Builder
- setSingleChoiceItems 单选有选框
- 多选列表对话框
- AlertDialog.Builder
- setMultiChoiceItems 可以多选
- 适配器(Adapter)对话框
- AlertDialog.Builder
- 普通列表对话框
生成方法结构图
每个弹窗代码:
- 普通的对话框
package com.fmy.daialog1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// finish();
}
});
builder.setNegativeButton("取消", null);
builder.setTitle("是否关闭");
builder.setMessage("确定关闭");
builder.show();
}
}
- 带进度条的对话框
package com.fmy.dialog2;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
ProgressDialog a = new ProgressDialog(this);
a.setIcon(R.drawable.ic_launcher);
a.setTitle("我就是个进度条");
a.setMessage("正在下");
a.setProgressStyle(ProgressDialog.STYLE_SPINNER);
a.setCancelable(false);
a.show();
a.setProgress(99);
}
}
- 日期选择对话框
package com.fmy.dialon;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click1(View v) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(calendar.YEAR);
int month = calendar.get(calendar.MONTH);
int day = calendar.get(calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
}, year, month, day);
dialog.show();
}
}
- 时间选择对话框
package com.fmy.dialon;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click2(View v) {
TimePickerDialog dialog = new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
}
}, 9 , 1, true);
dialog.show();
}
- 自定义的对话框
package com.fmy.dialog5;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
AlertDialog.Builder buidler = new AlertDialog.Builder(this);
buidler.setTitle("登录");
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.dialog, null);
buidler.setView(view);
buidler.setNegativeButton("取消", null);
buidler.setPositiveButton("确定", null);
buidler.show();
}
}
- 普通列表对话框
package com.fmy.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click1(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
builder.setItems(R.array.book, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "==which==", 0).show();
}
});
builder.create();
builder.show();
}
}
- 单选列表对话框
package com.fmy.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click2(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
builder.setSingleChoiceItems(getResources().getStringArray(R.array.book), 1, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "==which==" + which, 0).show();
}
});
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "==which==" + which, 0).show();
}
});
builder.create();
builder.show();
}
- 多选列表对话框
package com.fmy.fm;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
private String datas[];
private boolean flags[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datas = getResources().getStringArray(R.array.book);
flags = new boolean [datas.length];
}
public void click1(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("你好");
builder.setMultiChoiceItems(datas, flags, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which] = isChecked;
}
});
builder.show();
}
- 适配器(Adapter)对话框
package com.fmy.fm;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
private String datas[];
private boolean flags[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datas = getResources().getStringArray(R.array.book);
flags = new boolean [datas.length];
}
public void click2(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("嘿嘿");
String [] data = {"嘿嘿","你好不","我很好"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.adpater,R.id.tv,data);
builder.setAdapter(adapter, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
}