Dialog的简单使用

来源:互联网 发布:js九九乘法表原理 编辑:程序博客网 时间:2024/05/24 01:46

今天做一个小功能,Dialog的使用。一般来说,我们和用户交互所使用的类有这几个:Activity、Window、Dialog、Toast,可见Dialog的重用性,所以这是必须掌握的。先看下效果:




额,好像界面比较丑...,不过使用方法才是重点,下面我们就来实现一下。

首先是界面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ff0000"    android:orientation="vertical">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="60dp"        android:layout_marginTop="10dp">        <Button            android:id="@+id/bt_cancel"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentLeft="true"            android:layout_margin="5dp"            android:background="#00ff00"            android:text="取消"            android:textColor="#ffffff" />        <Button            android:id="@+id/bt_sure"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:layout_margin="5dp"            android:background="#00ff00"            android:text="确定"            android:textColor="#ffffff" />    </RelativeLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="60dp"        android:layout_margin="20dp"        android:background="#ffffff"        android:gravity="center"        android:text="我是弹出框"        android:textSize="16sp" /></LinearLayout>

然后是Activity:

public class MainActivity extends Activity implements View.OnClickListener {    //弹框dialog    private Dialog dialog;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.bt_click).setOnClickListener(this);    }    @Override    public void onClick(View v) {        if (v == null) {            return;        }        switch (v.getId()) {            case R.id.bt_click: //点击                showDialog();                break;            case R.id.bt_sure:  // 确定                clickSureButton();                break;            case R.id.bt_cancel:    //删除                clickCancelButton();                break;        }    }    /**     * 显示弹框     */    private void showDialog() {        dialog = new Dialog(this, R.style.clickDialogStyle);  //设置动画样式        dialog.show();        WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); //设置布局和对齐方式        params.width = WindowManager.LayoutParams.MATCH_PARENT;        params.height = WindowManager.LayoutParams.WRAP_CONTENT;        params.gravity = Gravity.BOTTOM;        dialog.getWindow().setAttributes(params);        dialog.setCancelable(true);        dialog.setContentView(R.layout.layout_show_dialog);        dialog.setCanceledOnTouchOutside(true);        //点击监听        dialog.findViewById(R.id.bt_sure).setOnClickListener(this);        dialog.findViewById(R.id.bt_cancel).setOnClickListener(this);    }    /**     * 弹框确定操作     */    private void clickSureButton() {        Toast.makeText(this, "我点击了确定", Toast.LENGTH_SHORT).show();        dialog.cancel();    }    /**     *  弹框取消操作     */    private void clickCancelButton() {        Toast.makeText(this, "我点击了取消", Toast.LENGTH_SHORT).show();        dialog.cancel();    }}

这里面可能就是弹出时的动画会比较麻烦,不过多写几次就好了,style:

<style name="dialog" 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">@android:color/transparent</item>        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:backgroundDimEnabled">true</item>        <item name="android:windowFullscreen">true</item>        <item name="android:backgroundDimAmount">0.4</item>    </style>    <style name="clickDialogStyle" parent="dialog">        <item name="android:windowAnimationStyle">@style/showDialogAnimationStyle</item>    </style>    <style name="showDialogAnimationStyle" parent="@android:style/Animation.Dialog">        <item name="android:windowEnterAnimation">@anim/bottom_in</item>        <item name="android:windowExitAnimation">@anim/bottom_out</item>    </style>

最后是我们的移动动画效果:

bottom_in:

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="300"    android:fromYDelta="100%"    android:toYDelta="0%" ></translate>

同理,bottom_out:

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="300"    android:fromYDelta="0%"    android:toYDelta="100%" ></translate>

ok啦,简单吧!当然,这只是比较简单的实现,在项目中我们可能会遇到比较复杂的,比如仿ios的滑轮效果,双级联动乃至三级联动的时间选择器等等,不过原理都是一样的。Tanks!



1 0