简单好用的自定义Dialog(二)

一、简单介绍
安卓提供的dialog的ui实在是丑,实际开发中我们通常自定义view实现我们需要的功能
先来张效果图;
这里写图片描述
二、具体实现
1)首先边框要是圆角的,不然太丑啦

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <corners
        android:radius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
    />
</shape>

android:radius=”5dp” 有了其实下面都不需要了。
2)布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <RelativeLayout
        android:id="@+id/re_tip_dialog"
        android:layout_width="250dp"
        android:layout_height="160dp"
        android:layout_centerInParent="true"
        android:background="@drawable/rounded_search_text">

        <ImageView
            android:layout_marginTop="10dp"
            android:layout_centerHorizontal="true"
            android:src="@drawable/laba"
            android:id="@+id/appTitle"
            android:layout_width="70dp"
            android:layout_height="70dp"
           />
        <LinearLayout
            android:layout_centerHorizontal="true"
            android:layout_below="@id/appTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tips1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我要举报"
                android:layout_marginTop="10dp"
                android:textColor="#ff0dadf8"
                />
            <TextView
                android:id="@+id/tips2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:textColor="#e70808"
                />
            <TextView
                android:id="@+id/tips3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="楼网友违规言论"
                android:layout_marginTop="10dp"
                android:textColor="#ff0dadf8"
                />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tips"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <Button
                android:layout_weight="1"
                android:id="@+id/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:text="我不举报了"
                android:textColor="#bcbcbc"
                android:textAlignment="center"
                android:background="@drawable/button_jubao"
                />
            <Button
                android:layout_weight="1"
                android:id="@+id/sure"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="确认举报"
                android:layout_below="@id/tips"
                android:textAlignment="center"
                android:textSize="12sp"
                android:background="@drawable/button_jubao"
                android:textColor="#494949"
           />

        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

3)具体的java类,在这里实现
package com.kkeji.news.client.view.Dialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkeji.news.client.R;

public class DialogInformAgainst extends Dialog implements
View.OnClickListener {
private onBtnClickListener onBtnClickListener;
private Context context;
private String sure;
private String cancle;
private String appTitle;
private String tips2;
private Button tv_dele_sure;
private Button tv_dele_cancle;
private TextView tv_tips1;
private TextView tv_tips2;
private TextView tv_tips3;
private RelativeLayout re_tip_dialog;

public DialogInformAgainst(Context context,
                           onBtnClickListener onBtnClickListener, String sure, String cancle,
                           String appTitle, String tips2) {
    super(context);
    this.onBtnClickListener = onBtnClickListener;
    this.context = context;
    this.tips2 = tips2;
    this.sure = sure;
    this.cancle = cancle;

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 去除默认的头部标题
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dialog_comment_jubao);
    tv_dele_sure = (Button) findViewById(R.id.sure);
    tv_dele_cancle = (Button) findViewById(R.id.cancel);
    tv_tips1 = (TextView) findViewById(R.id.tips1);
    tv_tips2 = (TextView) findViewById(R.id.tips2);
    tv_tips3 = (TextView) findViewById(R.id.tips3);

    re_tip_dialog = (RelativeLayout) findViewById(R.id.re_tip_dialog);

    tv_dele_cancle.setOnClickListener(this);
    tv_dele_sure.setOnClickListener(this);
    //进入动画是伪代码
    ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.0f,
            0.0f, 1.0f, Animation.RELATIVE_TO_SELF,
            0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    scaleAnimation.setDuration(200);
    re_tip_dialog.startAnimation(scaleAnimation);

    Window window = this.getWindow();
    WindowManager.LayoutParams params = this.getWindow().getAttributes();
    // 去除四角黑色背景
    window.setBackgroundDrawable(new BitmapDrawable());
    // 设置周围的暗色系数
    params.dimAmount = 0.5f;
    params.y=-280;
    window.setAttributes(params);

    // 为各个textview赋值
    tv_dele_sure.setText(sure);
    tv_dele_cancle.setText(cancle);
    tv_tips2.setText(tips2);
}

public interface onBtnClickListener {
    public void onSure();

    public void onExit();
}
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.cancel:

            onBtnClickListener.onExit();

            alertDialogExitAnim();
            break;
        case R.id.sure:
            onBtnClickListener.onSure();
            alertDialogExitAnim();
            break;

        default:
            break;
    }
}

private void alertDialogExitAnim() {
    ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f,
            1.0f, 0.0f, Animation.ABSOLUTE,
            re_tip_dialog.getWidth() / 2, Animation.ABSOLUTE,
            re_tip_dialog.getHeight() / 2);
    scaleAnimation.setDuration(1000);
    re_tip_dialog.startAnimation(scaleAnimation);

    scaleAnimation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            DialogInformAgainst.this.dismiss();
        }
    });
}

}
4)最后是实现调用,我是跟js交互写的,你们忽略就好

@JavascriptInterface
        public void postReport(final int pRid,String floor) {
             new DialogInformAgainst(ActivityArticleContent.this, new DialogInformAgainst.onBtnClickListener() {
                @Override
                public void onSure() {
                    mCommentsHelper.postUserReport(mNewsArticle.getArticle_id(), pRid, new CommentsHelper.GetNewsCommentsContent() {
                        //举报成功
                        @Override
                        public void onSuccess(int pStatusCode, String pRequestString) {
                            if (pStatusCode == 200) {
                                //成功
                                MLog.i(TAG, "postReport" + pRid + " ");
                                Toast.makeText(ActivityArticleContent.this, "谢谢您的举报,我们会尽快处理", Toast.LENGTH_SHORT).show();
                            }
                        }
                        @Override
                        public void onFailure(int pStatusCode) {
                            //失败
                            MLog.i(TAG, "postReport" + pRid + " error");
                        }
                    });
                }
                @Override
                public void onExit() {
                    Toast.makeText(ActivityArticleContent.this, "取消举报", Toast.LENGTH_SHORT).show();
                }
            }, "确定举报", "不举报了", "提示", floor+"").show();
        }

三、知识点梳理。
主要是在自定义java中写接口方法以实现回调。背景颜色,字体等能在自定义java中指定,也可以在调用的时候进行指定。diaolog的大小和显示位置都可以在自定义view中进行设置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值