初学android的时候,我们都是使用AlertDialog和它的Builder
// Dialog需要持有一个activity,所以该context必须是activity
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.xx); builder.setTitle(""); builder.setMessage("");
设置内容:
列表对话框 | setItems(int, OnClickListener) | setCursor(Cursor, OnClickListener, String) setAdapter(ListAdapter, OnClickListener) |
多选对话框 | setMultiChoiceItems(int, boolean[], OnMultiChoiceClickListener) | |
单选对话框 | setSingleChoiceItems(int, -1, OnClickListener) | |
自定义 | dialog.setContentView(view, 0, 0, 0, 0); //指定边框的和View的距离 |
监听处理:
builder.setCancelable(false) // 1、back键失效
builder.setOnCancelListener(new OnCancelListener() { // 2、处理back键
public void onCancel(DialogInterface dialog) {}
});
OnClickListener listener = new DialogInterface.OnClickListener() {
// 如果是setView的我们也可以直接给view.setOnClickListener
public void onClick(DialogInterface dialog, int which) { // dialog是当前对话框, which是点击的哪个按钮
switch (which) {
case DialogInterface.BUTTON_POSITIVE: Toast.makeText(MainActivity.this, "YES", 0).show(); break;
case DialogInterface.BUTTON_NEUTRAL: Toast.makeText(MainActivity.this, "CANCEL", 0).show();brea
case DialogInterface.BUTTON_NEGATIVE: Toast.makeText(MainActivity.this, "NO", 0).show();break; }
}
};
builder.setPositiveButton("YES", listener) // listener 为OnClickListener监听器对象, 监听按钮被选中
builder.setNeutralButton("CANCEL", listener) // builder.setNegativeButton("NO", listener) //
builder.show(); // builder.create().show();
dialog.cancle();dialog.dismiss();dialog = null; // 销毁对话框
以上是通常的写法。其实:
1、是AlertDialog继承自Dialog并添加上了很多style,比如说弹出框周围的黑框线。
2、无论Activity还是Dialog都是基于Window的,都是通过WindowManager的addView、removeView、updateView管理View,并通过WindoManager.LayoutParams设置属性的
3、Window规定了很多的级别,Toast等都有设定,最大好像是2002。Dialog创建的使用通过Windoww = PolicyManager.makeNewWindow(mContext);创建Window对象,并在dismiss时 mWindow.closeAllPanels();
那么现在我们可以这样写:
public class MainActivity extends Activity {
private Button testButton;
private Window mWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWindow = getWindow();
init();
}
private void init() {
testButton = (Button) this.findViewById(R.id.test1);
testButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new Dialog(MainActivity.this, R.style.CustomDialog);
dialog.setContentView(R.layout.dialog);
Window window = dialog.getWindow();
System.out.println(window == mWindow); // 不是同一个window
window.setWindowAnimations(R.style.dialog_anim_style);
dialog.show();
}
});
}
}
这里为Dialog的Window设置了两个动画,setWindowAnimations实际上是设置给了WindowManage.LayoutParams
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="dialog_anim_style_v14" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/dialog_enter</item>
<item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>
</resources>
入场动画:dialog_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<rotate
android:duration="210"
android:fromDegrees="-10"
android:interpolator="@android:anim/decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="1.5" />
<translate
android:duration="225"
android:fromYDelta="-100%p"
android:interpolator="@android:anim/decelerate_interpolator"
android:toYDelta="0%" />
</set>
出场动画:dialog_exit.xml
<?xmlversion="1.0" encoding="utf-8"?>
<setxmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:duration="300"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="10" />
<translate
android:duration="300"
android:fromYDelta="0%"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="100%p"/>
</set>
Dialog的布局为
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_dialog_bg"
android:padding="100dip" >
<TextView
android:id="@+id/test1"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
为了美观,我将Dialog的四周截取了圆角:shape_dialog_bg.xml
<?xmlversion="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" >
<cornersandroid:radius="@dimen/corner_small" /> 16dp
<solidandroid:color="@color/main_dark_green" /> #2198a2
</shape>
其中Dialog的样式设置为
<!-- Dialog -->
<stylename="CustomDialog" parent="@android:style/Theme.Dialog">
<itemname="android:windowBackground">@drawable/shape_dialog_panel</item>
<itemname="android:windowFrame">@null</item>
<itemname="android:windowIsFloating">true</item>
<itemname="android:windowIsTranslucent">true</item>
<itemname="android:windowNoTitle">true</item>
</style>
这里的背景设置为了透明色,因为在拆去圆角后会有几个尖角留下,为了和周围的界面一样故设置背景为透明色。
至此Dialog的去黑边和加动画搞定,因为是动画效果截图不明显,直接上附件。