Android笔记系列--AlertDialog基本使用
注意:
AlertDialog.Builder builder=new Builder(MainActivity.this);
这里的上下文对象必须是要显示对话框的当前Activity,不能是getApplicationContext()
AlertDialog 常见样式
//确定取消对话框
//1获取一个对话框的创建器
AlertDialog.Builder builder = new Builder(MainActivity.this);
//2所有builder设置一些参数
builder.setTitle("对话框标题");
builder.setMessage("提示是否退出");
builder.setPositiveButton("确定", new DialogInterface . OnClickListener () {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确定按钮被点击", 1).show();
}
});
builder.setNeutralButton("取消", new DialogInterface . OnClickListener () {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消按钮被点击", 1).show();
}
});
builder.create().show();
//pick对话框
AlertDialog.Builder builder2 = new Builder(MainActivity.this);
builder2.setTitle("选择一个人");
final String [] arr = new String[]{ "张三", "李四", "王五" };
builder2.setItems(arr, new DialogInterface . OnClickListener () {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, arr[which], 1).show();
}
});
builder2.create().show();
//带选择的单选按钮的对话框
AlertDialog.Builder builder3 = new Builder(MainActivity.this);
builder3.setTitle("选择一个颜色");
final String [] items = new String[]{ "蓝色", "黄色", "红色" };
builder3.setSingleChoiceItems(items, 1, new DialogInterface . OnClickListener () {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, items[which], 1).show();
}
});
builder3.create().show();
AlertDialog.Builder builder4 = new Builder(MainActivity.this);
builder4.setTitle("选择若干个颜色");
final String [] items4 = new String[]{ "蓝色", "黄色", "红色" };
builder4.setMultiChoiceItems(items4, new boolean []{ false, false, false }, new DialogInterface . OnMultiChoiceClickListener () {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, items4[which] + "选择状态" + isChecked, 1).show();
}
});
builder4.setPositiveButton("确定", new DialogInterface . OnClickListener () {
public void onClick(DialogInterface dialog, int which) {
}
});
builder4.create().show();
//进度条的对话框
ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("提示");
pd.setMessage("正在加载中,请稍后...");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置带进度条的
pd.setMax(100);
pd.show();
pd.setProgress(50);
AlertDialog中的EditText无法调出输入法的问题
方法一:使用Dialog代替
但这么一来,对话框的最外层会多出一个框,顶部还会空几十个DP,写一个style,在new Dialog的时候设置进去,把边框去掉。
<style name="CustomDialogStyle" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@null</item>
<item name="android:windowBackground">@null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.6</item>
</style>
android:windowFrame:界面对应的前景图片;
android:windowIsFloating:表示浮在屏幕上的,如果在这里使用了,整个layout就会在 屏幕中心,相当于浮在屏幕上,所以这个只适用于dialog
android:windowContentOverlay:表示标题栏的阴影部分的样式,使用图片或者颜色
android:windowNoTitle:标题栏是否隐藏,这就是我们上面显示的标题栏
android:windowIsFloating:表示浮在屏幕上的,如果在这里使用了,整个layout就会在 屏幕中心,相当于浮在屏幕上,所以这个只适用于dialog
android:windowContentOverlay:表示标题栏的阴影部分的样式,使用图片或者颜色
android:windowNoTitle:标题栏是否隐藏,这就是我们上面显示的标题栏
Dialog ad = new Dialog(context,R.style.CustomDialogStyle);
ad.setContentView(R.layout.dialog_test);
ad.show();
上面的写法显示的dialog不能适应屏幕的宽,改成下面:
Dialog dialog = new Dialog(MainActivity.this,R.style.CustomDialogStyle);
View view1 = getLayoutInflater().inflate(R.layout.dialog_test,null);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100);
dialog.setContentView(view1,layoutParams);
Window window = dialog.getWindow();
// 设置显示动画
window.setWindowAnimations(R.style.main_menu_animstyle);
WindowManager.LayoutParams wl = window.getAttributes();
// wl.x = 0;
// wl.y = getWindowManager().getDefaultDisplay().getHeight();
// 以下这两句是为了保证按钮可以水平满屏
wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
// 设置显示位置
dialog.onWindowAttributesChanged(wl);
dialog.show();
方法二:(没成功)
代码调出输入法
AlertDialog ad = new AlertDialog.Builder(MainActivity.this).create();
View view1 = getLayoutInflater().inflate(R.layout.dialog_test,null);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100);
view1.setLayoutParams(layoutParams);
ad.show();
ad.setContentView(view1);//AlertDialog 的setContentView只能在show之后调用,不然报错,但Dialog没有这个限制
public void showKeyboard() {
if(editText!=null){
//设置可获得焦点
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
//请求获得焦点
editText.requestFocus();
//调用系统输入法
InputMethodManager inputManager = (InputMethodManager) editText
.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);
}
}
方法三:AlertDialog使用setView而不是setContentView
Dialog 没有setView方法
AlertDialog ad = new AlertDialog.Builder(MainActivity.this).create();
View view1 = getLayoutInflater().inflate(R.layout.dialog_test,null);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100);
view1.setLayoutParams(layoutParams);
ad.setView(view1);
ad.show();
// ad.setContentView(view1);//setContentView只能在show之后调用,不然报错
DialogFragment 代替 Dialog
DialogFragment在android 3.0时被引入。使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其生命周期,保存临时数据,它和Fragment有着基本一致的声明周期。
使用DialogFragment至少需要实现onCreateView或者onCreateDIalog方法。
重写onCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.fragment_edit_name, container);
return view;
}
fragment_edit_name就是对话框的内容布局,不过默认对话框有个讨厌的标题,可以在onCreateView中调用getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);即可去掉。
重写onCreateDIalog
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_login_dialog, null);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(view)
// Add action buttons
.setPositiveButton("Sign in",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
}
}).setNegativeButton("Cancel", null);
return builder.create();
}
调用跟使用普通Fragment一样
LoginDialogFragment dialog = new LoginDialogFragment();
dialog.show(getFragmentManager(), "loginDialog");