Java和lidp,java中dp

自定义对话框不是这么搞啊

MyDialog.java

package com.antex.assist;

import android.app.Dialog;

import android.content.Context;

import android.content.DialogInterface;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.TextView;

import com.antex.R;

/**

*

package:

com.antex.assist

*

file:

MyDialog.java

*

*

Description:自定义dialog

*

创建时间:

2013-10

*

*

*

 
 

* @author xiaosanyu

*   email: 446251495@qq.com

*   blog: http://blog.csdn.net/a87b01c14

*

*

*/

public class MyDialog extends Dialog {

/**

* 构造函数

*

* @param context

* 上下文环境

* @param theme

* 主题

*/

public MyDialog(Context context, int theme) {

super(context, theme);

}

/**

* 构造函数

*

* @param context

* 上下文环境

*/

public MyDialog(Context context) {

super(context);

}

/**

* Helper class for creating a custom dialog

*/

public static class Builder {

/** 上下文环境 */

private Context context;

/** 标题 */

private String title;

/** 消息内容 */

private String message;

/** 左侧按钮文字 */

private String positiveButtonText;

/** 右侧按钮文字 */

private String negativeButtonText;

/** 中间按钮文字 */

private String neutralButtonText;

/** 子视图 */

private View contentView;

/** 标题图标 */

private int titleimg = 0;

/** 左侧按钮点击监听 */

private DialogInterface.OnClickListener positiveButtonClickListener;

/** 中间按钮点击监听 */

private DialogInterface.OnClickListener neutralButtonClickListener;

/** 右侧按钮点击监听 */

private DialogInterface.OnClickListener negativeButtonClickListener;

/**

* 构造器

*

* @param context

* 上下文环境

*/

public Builder(Context context) {

this.context = context;

}

/**

* Set the Dialog message from String

*

* @param message

* String类型对话框消息

* @return Builder 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setMessage(String message) {

this.message = message;

return this;

}

/**

* Set the Dialog message from resource

*

* @param message

* int型根据ID获取会话框消息

* @return Builder 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setMessage(int message) {

this.message = (String) context.getText(message);

return this;

}

/**

* Set the Dialog title from resource

*

* @param title

* int型根据ID获取对话框标题

* @return Builder 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setTitle(int title) {

this.title = (String) context.getText(title);

return this;

}

/**

* Set the Dialog titleimg from resource

*

* @param titleimg

* 对话框图标

* @return Builder 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setIcon(int titleimg) {

this.titleimg = titleimg;

return this;

}

/**

* Set the Dialog title from String

*

* @param title

* String型对话框标题

* @return Builder 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setTitle(String title) {

this.title = title;

return this;

}

/**

* Set a custom content view for the Dialog. If a message is set, the

* contentView is not added to the Dialog...

*

* @param v

* 对话框子视图

* @return Builer 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setContentView(View v) {

this.contentView = v;

return this;

}

/**

* Set the positive button resource and it's listener

*

* @param positiveButtonText

* 按钮文本

* @param listener

* 按钮点击监听

* @return Builer 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setPositiveButton(int positiveButtonText,

DialogInterface.OnClickListener listener) {

this.positiveButtonText = (String) context

.getText(positiveButtonText);

this.positiveButtonClickListener = listener;

return this;

}

/**

* Set the positive button text and it's listener

*

* @param positiveButtonText

* 按钮文本

* @param listener

* 按钮点击监听

* @return Builer 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setPositiveButton(String positiveButtonText,

DialogInterface.OnClickListener listener) {

this.positiveButtonText = positiveButtonText;

this.positiveButtonClickListener = listener;

return this;

}

/**

* Set the negative button resource and it's listener

*

* @param negativeButtonText

* 按钮文本

* @param listener

* 按钮点击监听

* @return Builer 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setNegativeButton(int negativeButtonText,

DialogInterface.OnClickListener listener) {

this.negativeButtonText = (String) context

.getText(negativeButtonText);

this.negativeButtonClickListener = listener;

return this;

}

/**

* Set the negative button text and it's listener

*

* @param negativeButtonText

* 按钮文本

* @param listener

* 按钮点击监听

* @return Builer 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setNegativeButton(String negativeButtonText,

DialogInterface.OnClickListener listener) {

this.negativeButtonText = negativeButtonText;

this.negativeButtonClickListener = listener;

return this;

}

/**

* Set the Neutral button resource and it's listener

*

* @param neutralButtonText

* 按钮文本

* @param listener

* 按钮点击监听

* @return Builer 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setNeutralButton(int neutralButtonText,

DialogInterface.OnClickListener listener) {

this.neutralButtonText = (String) context

.getText(neutralButtonText);

this.negativeButtonClickListener = listener;

return this;

}

/**

* Set the Neutral button text and it's listener

*

* @param neutralButtonText

* 按钮文本

* @param listener

* 按钮点击监听

* @return Builer 是个抽象类。用于抽象创建下属dialog等类

*/

public Builder setNeutralButton(String neutralButtonText,

DialogInterface.OnClickListener listener) {

this.neutralButtonText = neutralButtonText;

this.neutralButtonClickListener = listener;

return this;

}

/**

* Create the custom dialog

*

* @return MyDialog 返回自定义Dialog

*/

public MyDialog create() {

LayoutInflater inflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// instantiate the dialog with the custom Theme

final MyDialog dialog = new MyDialog(context, R.style.MyDialog);

View layout = inflater.inflate(R.layout.dialog, new LinearLayout(

context), false);

ImageView titleimage = (ImageView) layout

.findViewById(R.id.dialog_title_image);

TextView tv = (TextView) layout.findViewById(R.id.dialog_msg);

Button positiveButton = (Button) layout

.findViewById(R.id.positiveButton);

Button negativeButton = (Button) layout

.findViewById(R.id.negativeButton);

Button neutralButton = (Button) layout

.findViewById(R.id.neutralButton);

dialog.addContentView(layout, new LayoutParams(

LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

// set the dialog title

if (title != null)

((TextView) layout.findViewById(R.id.dialog_title))

.setText(title);

if (titleimg != 0)

titleimage.setBackgroundResource(titleimg);

else

titleimage.setVisibility(View.GONE);

// set the confirm button

if (positiveButtonText != null) {

positiveButton.setText(positiveButtonText);

if (positiveButtonClickListener != null) {

positiveButton

.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

positiveButtonClickListener.onClick(dialog,

DialogInterface.BUTTON_POSITIVE);

}

});

}

} else {

// if no confirm button just set the visibility to GONE

positiveButton.setVisibility(View.GONE);

}

// set the cancel button

if (negativeButtonText != null) {

negativeButton.setText(negativeButtonText);

if (negativeButtonClickListener != null) {

negativeButton

.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

negativeButtonClickListener.onClick(dialog,

DialogInterface.BUTTON_POSITIVE);

}

});

}

} else {

// if no confirm button just set the visibility to GONE

negativeButton.setVisibility(View.GONE);

}

// set the neutral button

if (neutralButtonText != null) {

neutralButton.setText(neutralButtonText);

LayoutParams lp = positiveButton.getLayoutParams();

lp.width = convertDIP2PX(80);

positiveButton.setLayoutParams(lp);

RelativeLayout.LayoutParams contentLayoutParams = new RelativeLayout.LayoutParams(

convertDIP2PX(80),

RelativeLayout.LayoutParams.WRAP_CONTENT);

contentLayoutParams.leftMargin = convertDIP2PX(15);

contentLayoutParams.addRule(RelativeLayout.RIGHT_OF,

R.id.neutralButton);

negativeButton.setLayoutParams(contentLayoutParams);

if (neutralButtonClickListener != null) {

neutralButton

.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

neutralButtonClickListener.onClick(dialog,

DialogInterface.BUTTON_POSITIVE);

}

});

}

} else {

// if no neutral button just set the visibility to GONE

neutralButton.setVisibility(View.GONE);

}

// set the content message

if (message != null) {

tv.setText(message);

}

if (contentView != null) {

// add the contentView to the dialog body

if (message == null || message == "")

tv.setVisibility(View.GONE);

LinearLayout ll = (LinearLayout) layout

.findViewById(R.id.content);

// WindowManager manager = (WindowManager) context

// .getSystemService(Context.WINDOW_SERVICE);

// Display display = manager.getDefaultDisplay();

// int width = display.getWidth();

LinearLayout.LayoutParams contentLayoutParams = new LinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

contentLayoutParams.weight = 0;

// contentLayoutParams.gravity=Gravity.CENTER_VERTICAL;

contentLayoutParams.leftMargin = 30;

contentLayoutParams.rightMargin = 30;

contentLayoutParams.topMargin = 10;

ll.addView(contentView, contentLayoutParams);

}

dialog.setContentView(layout);

return dialog;

}

/**

* 转换dip为px

*

* @param dip

* dip大小

* @return int dip单位转换成px大小

*/

public int convertDIP2PX(int dip) {

float scale = context.getResources().getDisplayMetrics().density;

return (int) (dip * scale + 0.5f);

}

}

}

布局文件

dialog.xml

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/dialog_bg"

android:gravity="center_horizontal"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:paddingLeft="20dip" >

android:id="@+id/dialog_title_image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="15dip"

android:layout_marginTop="15dip"

android:background="@drawable/dialog_title_image"

android:contentDescription="@string/button_back" />

android:id="@+id/dialog_title"

style="@style/DialogText.Title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="19dp"

android:layout_marginTop="20dip"

android:layout_toRightOf="@+id/dialog_title_image" />

android:layout_width="wrap_content"

android:layout_height="1dip"

android:layout_marginLeft="10dip"

android:layout_marginRight="10dip"

android:layout_marginTop="5dip"

android:background="@drawable/lins" />

android:layout_width="fill_parent"

android:layout_height="0dip"

android:layout_weight="1"

android:fillViewport="true" >

android:id="@+id/content"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

android:id="@+id/dialog_msg"

style="@style/DialogText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="15dip"

android:layout_marginRight="5dip"

android:layout_marginTop="10dip" />

android:id="@+id/drl"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="bottom|center_horizontal"

android:paddingBottom="30dip"

android:paddingTop="10dip" >

android:id="@+id/positiveButton"

android:layout_width="100dip"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:background="@drawable/button_selector"

android:text="@string/button_ok"

android:textColor="@color/black"

android:textSize="20sp"

android:textStyle="bold" />

android:id="@+id/neutralButton"

android:layout_width="80dip"

android:layout_height="wrap_content"

android:layout_marginLeft="15dip"

android:layout_toRightOf="@id/positiveButton"

android:background="@drawable/button_selector"

android:textColor="@color/black"

android:textSize="20sp"

android:textStyle="bold" />

android:id="@+id/negativeButton"

android:layout_width="100dip"

android:layout_height="wrap_content"

android:layout_marginLeft="30dip"

android:layout_toRightOf="@id/neutralButton"

android:background="@drawable/button_selector"

android:text="@string/button_cancel"

android:textColor="@color/black"

android:textSize="20sp"

android:textStyle="bold" />

style

@null

true

@drawable/dialog_bg

true

@null

#FF000000

20sp

bold

25sp

bold

#1E90FF

调用

MyDialog.Builder customBuilder = new MyDialog.Builder(mContext0);

LayoutInflater layout =getLayoutInflater();

View longinDialogView = layout.inflate(R.layout.logindialog, null, false);

//获取布局中的控件

EditText mUserName = (EditText)longinDialogView.findViewById( MResource.getIdByName(getApplication(), "id", "edit_username"));

EditText mPassword = (EditText)longinDialogView.findViewById( MResource.getIdByName(getApplication(), "id", "edit_password"));

customBuilder.setTitle("").setContentView(view).setPositiveButton(“确定”, new

DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}

}).setNegativeButton(“取消”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}

});

Dialog dialog = customBuilder.create();

dialog.show();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值