自定义底部弹出Dialog(老掉牙的需求还是记录一下吧)

本文介绍了一个自定义Android对话框的过程,包括布局设计、背景样式、按钮状态选择器及动画效果等细节,展示了如何通过XML资源文件实现美观且交互良好的用户界面。

这里写图片描述
(1)布局:
phonto_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/choice_dialog1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape_win_photo_bg"
    android:orientation="vertical"
    android:padding="20dp" >


    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/selector_photo_bg"
        android:gravity="center"
        android:text="拍照"
        android:textSize="15sp" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/selector_photo_bg"
        android:gravity="center"
        android:text="从相册中选择"
        android:textSize="15sp" />

    <Button
        android:id="@+id/btn_cancle_photo"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/selector_photo_bg"
        android:gravity="center"
        android:text="取消"
        android:textSize="15sp" />


</LinearLayout>

其中:
shape_win_photo_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

    <solid android:color="#404848" />

</shape>

selector_photo_bg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
            <solid android:color="#d81e06" />
        </shape>
    </item>

       <item android:state_focused="true">
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
            <solid android:color="#d81e06" />
        </shape>
    </item>

    <item >
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
            <solid android:color="#D0D0D0" />
        </shape>
    </item>
</selector>

java中调用

    Dialog dialog = null;
    public void selectImg(){

        if(dialog != null && dialog.isShowing()){
            dialog.dismiss();
            return;
        }
        dialog = new Dialog(this);
        Button select_poto_btn, camera_poto_btn,btn_cancle_photo;
        View view = LayoutInflater.from(this).inflate(R.layout.phonto_dialog, null);
        select_poto_btn = (Button) view.findViewById(R.id.btn1);
        camera_poto_btn = (Button) view.findViewById(R.id.btn2);
        btn_cancle_photo = (Button) view.findViewById(R.id.btn_cancle_photo);
        select_poto_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startImgIntent(SELECT_FROM_IMGS_REQUESTCODE);
                dialog.dismiss();

            }
        });
        camera_poto_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                startTakePhoto();
                dialog.dismiss();
            }
        });

        btn_cancle_photo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });


        dialog.setContentView(view);
        WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        attributes.gravity = Gravity.BOTTOM;
        attributes.windowAnimations = R.style.mydialogstyle;
        dialog.getWindow().setAttributes(attributes);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.show();
    }

其中:
mydialogstyle

  <style name="mydialogstyle" >
        <item name="@android:windowEnterAnimation">@anim/push_bottom_in</item>
        <item name="@android:windowExitAnimation">@anim/push_bottom_out</item>
    </style>

其中push_bottom_in

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromYDelta="100%p"
        android:toYDelta="0"        
     />      
</set>

push_bottom_out

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑出式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >


    <translate
        android:duration="500"
        android:fromYDelta="0"
        android:toYDelta="100%p" />
</set>
package com.example.animdialogdemo; import com.example.calculatedemo.R; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class CalculatorDialog extends Dialog implements OnClickListener { private Button[] btn = new Button[10]; private EditText etLed; private Button btnSub, btnPlus, btnEqual, btnDot, btnC, mButton_cancel, btnOk; private double predata = 0; private String preopt = "="; private boolean vbegin = true; private int i = 1; public CalculatorDialog(Context context) { super(context); // TODO Auto-generated constructor stub this.show(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_caclu); findUiById(); addListener(); } private void findUiById() { etLed = (EditText) this.findViewById(R.id.ed_led); btn[0] = (Button) this.findViewById(R.id.mButton0); btn[1] = (Button) this.findViewById(R.id.mButton1); btn[2] = (Button) this.findViewById(R.id.mButton2); btn[3] = (Button) this.findViewById(R.id.mButton3); btn[4] = (Button) this.findViewById(R.id.mButton4); btn[5] = (Button) this.findViewById(R.id.mButton5); btn[6] = (Button) this.findViewById(R.id.mButton6); btn[7] = (Button) this.findViewById(R.id.mButton7); btn[8] = (Button) this.findViewById(R.id.mButton8); btn[9] = (Button) this.findViewById(R.id.mButton9); btnSub = (Button) this.findViewById(R.id.mButton_jia); btnPlus = (Button) this.findViewById(R.id.mButton_jian); btnEqual = (Button) this.findViewById(R.id.mButton_equ); btnDot = (Button) this.findViewById(R.id.mButton_dian); btnC = (Button) findViewById(R.id.mButton_C); btnOk = (Button) this.findViewById(R.id.mButton_ok); mButton_cancel = (Button) this.findViewById(R.id.mButton_cancel); } public void addListener() { for (int i = 0; i < btn.length; i++) { btn[i].setOnClickListener(this); } btnOk.setOnClickListener(this); btnSub.setOnClickListener(this); btnPlus.setOnClickListener(this); btnEqual.setOnClickListener(this); btnDot.setOnClickListener(this); btnC.setOnClickListener(this); mButton_cancel.setOnClickListener(this); } public void onClick(View v) { String command = ((Button) v).getText().toString(); String str = etLed.getText().toString(); if (command.compareTo("bac") == 0) { if (str.length() > 1) { etLed.setText(str.substring(0, str.length() - 1)); } else if (str.length() == 1) { etLed.setText("0"); vbegin = true; } } else if (command.compareTo("C") == 0) { etLed.setText("0"); vbegin = true; predata = 0; preopt = "="; } else if ("0123456789".indexOf(command) != -1) { wtNumber(command); } else if ("+-=".indexOf(command) != -1) { wtOperater(command); } if ("ȷ��".equals(command)) { this.dismiss(); } switch (v.getId()) { case R.id.mButton_cancel: dismiss(); break; default: break; } } private void wtNumber(String str) { if (vbegin) { etLed.setText(str); } else { etLed.append(str); } vbegin = false; } private void wtOperater(String opt) { try { double temp = Double.parseDouble(etLed.getText().toString()); if (vbegin) { preopt = opt; } else { if (preopt.equals("=")) { predata = temp; } else if (preopt.equals("+")) { predata += temp; } } etLed.setText(predata + ""); preopt = opt; } catch (NumberFormatException e) { etLed.setText("����Ϊ0"); } catch (ArithmeticException e) { etLed.setText("����Ϊ0"); preopt = "="; } finally { vbegin = true; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值