自定义弹窗 (一)

效果图


  这是比较简单的自定义弹窗应用,后面将有更多的自定义弹窗应用。废话不多说直接上代码


Main

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button clickbtn1 = (Button) this.findViewById(R.id.clickbtn1);
		Button clickbtn2 = (Button) this.findViewById(R.id.clickbtn2);
		Button clickbtn3 = (Button) this.findViewById(R.id.clickbtn3);
		clickbtn1.setOnClickListener(listener);
		clickbtn2.setOnClickListener(listener);
		clickbtn3.setOnClickListener(listener);
	}

	private OnClickListener listener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			switch (v.getId())
			{
			case R.id.clickbtn1:
				AlertHelper.showTips(Main.this, "某某提示您", "  <font color=\"red\">含关闭按钮、含取消按钮</font>", true, true, "确定", "关闭");
				break;
			case R.id.clickbtn2:
				AlertHelper.showTips(Main.this, "某某提示您", "  <font color=\"red\">含关闭按钮、无取消按钮</font>", false, true, "确定", "关闭");
				break;
			case R.id.clickbtn3:
				AlertHelper.showTips(Main.this, "某某提示您", "  <font color=\"red\">无关闭按钮、无取消按钮</font>", false, false, "确定", null);
				break;
			}
		}
	};
}

AlertHelper

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class AlertHelper
{
	/** 微软雅黑默认 */
	public static Typeface typefacenormal = Typeface.create("微软雅黑", Typeface.NORMAL);
	/** 微软雅黑粗体 */
	public static Typeface typefacebold = Typeface.create("微软雅黑", Typeface.BOLD);

	/**
	 * 弹出提示窗
	 * 
	 * @param titlestring
	 *            提示标题
	 * @param message
	 *            提示信息
	 * @param isshowcancle
	 *            是否显示取消按钮
	 * @param isshowclose
	 *            是否显示关闭按钮
	 * @param okstring
	 *            确定按钮字符
	 * @param canclestring
	 *            取消按钮字符
	 */
	public static void showTips(Context context, String titlestring, String message, boolean isshowcancle, boolean isshowclose, String okstring, String canclestring)
	{
		final AlertDialog dlg = new AlertDialog.Builder(context).setCancelable(false).create();
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.alertdialog_showtxt);
		TextView title = (TextView) window.findViewById(R.id.AlertDialogTitle);
		Button btn_close = (Button) window.findViewById(R.id.btn_close);
		TextView text = (TextView) window.findViewById(R.id.AlertDialogText);
		Button ok = (Button) window.findViewById(R.id.btn_ok);
		Button cancel = (Button) window.findViewById(R.id.btn_cancel);
		title.setTypeface(typefacenormal);
		text.setTypeface(typefacenormal);
		ok.setTypeface(typefacebold);
		cancel.setTypeface(typefacebold);
		title.setText(titlestring);
		text.setText(Html.fromHtml(message));
		if (isshowcancle)
		{
			cancel.setVisibility(View.VISIBLE);
			cancel.setText(canclestring);
		} else
		{
			cancel.setVisibility(View.GONE);
		}
		if (isshowclose)
		{
			btn_close.setVisibility(View.VISIBLE);
		} else
		{
			btn_close.setVisibility(View.GONE);
		}
		ok.setText(okstring);
		ok.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				dlg.cancel();
			}
		});
		cancel.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				dlg.cancel();
			}
		});
		btn_close.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				dlg.cancel();
			}
		});
	}
}

main.xml

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

    <RelativeLayout
        android:id="@+id/AlertDialogTop_Layout"
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:background="@drawable/alertdialogtop_bg" >

        <!-- 弹窗Title -->

        <TextView
            android:id="@+id/AlertDialogTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:textColor="@color/skyblue"
            android:textSize="24sp" />

        <Button
            android:id="@+id/btn_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/alertdialog_close" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/AlertDialogContent_Layout"
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/AlertDialogTop_Layout"
        android:background="@drawable/alertdialog_bg" >

        <TextView
            android:id="@+id/AlertDialogText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:layout_margin="10dp"
            android:textColor="@color/black"
            android:textSize="18sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/AlertDialogEnd_Layout"
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/AlertDialogContent_Layout"
        android:background="@drawable/alertdialogend_bg" >

        <RelativeLayout
            android:id="@+id/AlertDialogBtn_Layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_margin="5dp" >

            <!-- 确认按钮 -->

            <Button
                android:id="@+id/btn_ok"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/alertdialog_btn_bg"
                android:textColor="@color/black"
                android:textSize="20sp" />
            <!-- 取消按钮 -->

            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_toRightOf="@+id/btn_ok"
                android:background="@drawable/alertdialog_btn_bg"
                android:textColor="@color/black"
                android:textSize="20sp" />
        </RelativeLayout>
    </RelativeLayout>

</RelativeLayout>

由于我用的是平板测试,layout_width你可以自定义设大小 我这里500dp

本例下载

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值