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

来源:互联网 发布:手机能用淘宝助理吗 编辑:程序博客网 时间:2024/05/20 10:56

一、简单介绍
安卓提供的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;}@Overrideprotected 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();}@Overridepublic 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