1、普通的对话框
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" > <ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
Dialog_progress.xml
public class MyDialog extends Dialog{ //必须要给构造方法
public MyDialog(Context context) {
//也可以在构建Dialog对象的时候就给指定Dialog样式
//使用主题来修改Dialog样式,在res/values/styles.xml中添加自定义主题
super(context,R.style.DialogTheme);
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//这个可以不要标题。通过getWindow().requestFeature(featureId)方法
//getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_progress);
} /**
* 一个Activity或者一个Dialog刚刚出现在用户面前的时候,焦点改变调用onWindowFocusChanged
*/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
}
}
MyDialog
// 普通对话框
public void dialog1(View v) {
MyDialog dialog = new MyDialog(this);
dialog.setTitle("这是进度Dialog");
// 显示对话框
dialog.show(); // 关闭对话框用
// dialog.dismiss();
}
普通对话框
2、警告对话框AlertDialog setMessage
//AlertDialog setMessage
public void dialog2(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("警告!").setIcon(R.drawable.ic_launcher)
.setMessage("前方高能")
// 注意这个导的包是import android.content.DialogInterface.OnClickListener;
.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了OK",
Toast.LENGTH_SHORT).show(); }
})
.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了Cancel",
Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("Ignore", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了Ignore",
Toast.LENGTH_SHORT).show();
}
})
.create().show();
}
警告对话框,setMessage
3、菜单对话框AlertDialog setItem
//菜单选择, setItem 如果设置setMessage,那么只会显示Message
String[] setting = {"声音","存储","显示","应用","语言和输入法","流量使用情况","WLAN"};
public void dialog3(View v){
new AlertDialog.Builder(this)
.setTitle("设置")
.setIcon(R.drawable.setting)
//which代表第几项,item点击后自动关闭,不需要Button
.setItems(setting, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, setting[which], Toast.LENGTH_SHORT).show();
}
})
.create().show();
}
菜单对话框 setItem
4、单选对话框AlertDialog setSingleChoiceItems
//单选对话框,setItem
String[] hobby = {"唱歌","跑步","武术","乒乓球","敲代码"};
int choice = 0;
public void dialog4(View v){
new AlertDialog.Builder(this)
.setTitle("爱好单选")
.setIcon(R.drawable.hobby)
//0代表默认选中第一个,选中不会自动关闭
.setSingleChoiceItems(hobby, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
choice = which;
}
})
//Button上的which永远为0,所以这里需要一个变量来保存选中的ItemID
.setPositiveButton("ok", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, hobby[choice], Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("cancel",null)
.create().show();
}
单选对话框 setSingleChoiceItems
5、多选对话框AlertDialog setMultiChoiceItem
String[] hobby = {"唱歌","跑步","武术","乒乓球","敲代码"};
boolean[] bool = {false,false,false,false,false};
List<String> list = new ArrayList<String>();
public void dialog5(View v){
new AlertDialog.Builder(this)
.setTitle("爱好可多选")
.setIcon(R.drawable.hobby)
//默认选中了哪些,点击也不会自动关闭
.setMultiChoiceItems(hobby, bool, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
list.add(hobby[which]);
}else{
list.remove(hobby[which]);
}
}
})
.setPositiveButton("ok", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, list.toString(), Toast.LENGTH_SHORT).show(); }
}).create().show();
}
多选对话框AlertDialog setMultiChoiceItem
6、适配器对话框AlertDialog setAdapter
public void dialog6(View v){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hobby);
new AlertDialog.Builder(this)
.setTitle("适配器对话框")
//和setItem一样,选中之后对话框就自动消失,不需要Button
.setAdapter(adapter, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, hobby[which], Toast.LENGTH_SHORT).show(); }
}).create().show();
}
适配器对话框AlertDialog setAdapter
7、自定义对话框AlertDialog setView
自定义对话框AlertDialog setView
8、关闭对话框AlertDialog dismisson
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="关闭对话框请点击关闭按钮"/> <TextView
android:id="@+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"/> </LinearLayout>
Dialog_dismiss.xml
public void dialog8(View v){
View layout = getLayoutInflater().inflate(R.layout.dialog_dismiss, null);
TextView finish = (TextView) layout.findViewById(R.id.finish);
final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("可关闭的对话框")
.setView(layout)
.create();
dialog.show();
finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
}); //有时候用户可能点到了外部,dialog就直接关闭了,而程序不知道,这时候就需要设置
dialog.setCancelable(false);
dialog.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Toast.makeText(MainActivity.this, "关闭", Toast.LENGTH_SHORT).show(); }
});
}
关闭对话框 AlertDialog setView