由于时间原因,没有详细整理,直接拿网上代码:
先看看效果:
首先,布局文件:
activity_main.xml:
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
"match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
customdialog.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:minWidth="200dp"
android:orientation="vertical"
android:scrollbars="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:text="@string/prompt"
android:textColor="@android:color/black"
android:textSize="18sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="10dp"
android:minHeight="40dp"
android:orientation="vertical" >
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="@string/exit_app"
android:textColor="@android:color/black"
android:textSize="18sp" />
RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#D1D1D1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:orientation="horizontal" >
<Button
android:id="@+id/cancel_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/left_btn_select"
android:text="@string/cancel"
android:textSize="20sp"
android:textColor="@color/btntextcolor" />
<LinearLayout
android:id="@+id/single_line"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#D1D1D1"
android:orientation="horizontal" />
<Button
android:id="@+id/neutral_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/left_btn_select"
android:text="@string/dismiss"
android:textSize="20sp"
android:textColor="@color/btntextcolor" />
<LinearLayout
android:id="@+id/second_line"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#D1D1D1"
android:orientation="horizontal" />
<Button
android:id="@+id/confirm_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/right_btn_select"
android:text="@string/confirm"
android:textSize="20sp"
android:textColor="@color/btntextcolor" />
LinearLayout>
LinearLayout>
ScrollView>
z再次,colors.xml:
<color name="black">#000000color>
<color name="btntextcolor">#ff1E90FFcolor>
strings.xml:
<string name="action_settings">Settingsstring>
<string name="single_uialertview">IOS UIAlertView(单个按钮)string>
<string name="single_alertdialog">Android AlertDialog(单个按钮)string>
<string name="double_uialertview">IOS UIAlertView(两个按钮)string>
<string name="double_alertdialog">Android AlertDialog(两个按钮)string>
<string name="three_uialertview">IOS UIAlertView(三个按钮)string>
<string name="three_alertdialog">Android AlertDialog(三个按钮)string>
<string name="prompt">提示string>
<string name="exit_app">你确定要退出应用吗?string>
<string name="confirm">确定string>
<string name="cancel">取消string>
<string name="dismiss">隐藏string>
styles.xml:
z再次,drawable文件:
bg.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:endColor="#ffffff"
android:startColor="#ffffff" />
<solid android:color="#ffffff" />
<corners
android:bottomLeftRadius="15px"
android:bottomRightRadius="15px"
android:topLeftRadius="15px"
android:topRightRadius="15px" />
shape>
check_left_btn.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#EAEAEA" />
<corners android:bottomLeftRadius="15px" />
shape>
check_right_btn.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#EAEAEA" />
<corners android:bottomRightRadius="15px" />
shape>
check_single_btn.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#EAEAEA" />
<corners
android:bottomLeftRadius="15px"
android:bottomRightRadius="15px" />
shape>
left_btn.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:bottomLeftRadius="15px" />
shape>
left_btn_select.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/check_left_btn" android:state_pressed="true"/>
<item android:drawable="@drawable/left_btn" android:state_focused="false" android:state_pressed="false"/>
<item android:drawable="@drawable/check_left_btn" android:state_focused="true"/>
<item android:drawable="@drawable/left_btn" android:state_focused="false"/>
selector>
right_btn.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:bottomRightRadius="15px" />
shape>
right_btn_select.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/check_right_btn" android:state_pressed="true"/>
<item android:drawable="@drawable/right_btn" android:state_focused="false" android:state_pressed="false"/>
<item android:drawable="@drawable/check_right_btn" android:state_focused="true"/>
<item android:drawable="@drawable/right_btn" android:state_focused="false"/>
selector>
single_btn.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners
android:bottomLeftRadius="15px"
android:bottomRightRadius="15px" />
shape>
single_btn_select.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/check_single_btn" android:state_pressed="true"/>
<item android:drawable="@drawable/single_btn" android:state_focused="false" android:state_pressed="false"/>
<item android:drawable="@drawable/check_single_btn" android:state_focused="true"/>
<item android:drawable="@drawable/single_btn" android:state_focused="false"/>
selector>
CustomDialog:
public class CustomDialog extends Dialog {
public CustomDialog(Context context) {
super(context);
}
public CustomDialog(Context context, int theme) {
super(context, theme);
}
public static class Builder {
private Context context; // 上下文对象
private String title; // 对话框标题
private String message; // 对话框内容
private String confirm_btnText; // 按钮名称“确定”
private String cancel_btnText; // 按钮名称“取消”
private String neutral_btnText; // 按钮名称“隐藏”
private View contentView; // 对话框中间加载的其他布局界面
/* 按钮坚挺事件 */
private OnClickListener confirm_btnClickListener;
private OnClickListener cancel_btnClickListener;
private OnClickListener neutral_btnClickListener;
public Builder(Context context) {
this.context = context;
}
/* 设置对话框信息 */
public Builder setMessage(String message) {
this.message = message;
return this;
}
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
*
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog title from String
*
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* 设置对话框界面
*
* @param v
* View
* @return
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* Set the positive button resource and it's listener
*
* @param confirm_btnText
* @return
*/
public Builder setPositiveButton(int confirm_btnText,
OnClickListener listener) {
this.confirm_btnText = (String) context.getText(confirm_btnText);
this.confirm_btnClickListener = listener;
return this;
}
/**
* Set the positive button and it's listener
*
* @param confirm_btnText
* @return
*/
public Builder setPositiveButton(String confirm_btnText,
OnClickListener listener) {
this.confirm_btnText = confirm_btnText;
this.confirm_btnClickListener = listener;
return this;
}
public Builder setNegativeButton(int cancel_btnText,
OnClickListener listener) {
this.cancel_btnText = (String) context.getText(cancel_btnText);
this.cancel_btnClickListener = listener;
return this;
}
public Builder setNegativeButton(String cancel_btnText,
OnClickListener listener) {
this.cancel_btnText = cancel_btnText;
this.cancel_btnClickListener = listener;
return this;
}
public Builder setNeutralButton(int neutral_btnText,
OnClickListener listener) {
this.neutral_btnText = (String) context.getText(neutral_btnText);
this.neutral_btnClickListener = listener;
return this;
}
public Builder setNeutralButton(String neutral_btnText,
OnClickListener listener) {
this.neutral_btnText = neutral_btnText;
this.neutral_btnClickListener = listener;
return this;
}
public CustomDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final CustomDialog dialog = new CustomDialog(context,
R.style.mystyle);
View layout = inflater.inflate(R.layout.customdialog, null);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
((TextView) layout.findViewById(R.id.title)).setText(title);
((TextView) layout.findViewById(R.id.title)).getPaint()
.setFakeBoldText(true);
if (title == null || title.trim().length() == 0) {
((TextView) layout.findViewById(R.id.message))
.setGravity(Gravity.CENTER);
}
if (neutral_btnText != null && confirm_btnText != null
&& cancel_btnText != null) {
((Button) layout.findViewById(R.id.confirm_btn))
.setText(confirm_btnText);
if (neutral_btnClickListener != null) {
((Button) layout.findViewById(R.id.neutral_btn))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
neutral_btnClickListener.onClick(dialog,
DialogInterface.BUTTON_NEUTRAL);
}
});
} else {
((Button) layout.findViewById(R.id.neutral_btn))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
} else {
// if no confirm button or cancle button or neutral just set the
// visibility to GONE
layout.findViewById(R.id.neutral_btn).setVisibility(View.GONE);
layout.findViewById(R.id.single_line).setVisibility(View.GONE);
}
// set the confirm button
if (confirm_btnText != null) {
((Button) layout.findViewById(R.id.confirm_btn))
.setText(confirm_btnText);
if (confirm_btnClickListener != null) {
((Button) layout.findViewById(R.id.confirm_btn))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
confirm_btnClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
} else {
((Button) layout.findViewById(R.id.confirm_btn))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.confirm_btn).setVisibility(View.GONE);
layout.findViewById(R.id.second_line).setVisibility(View.GONE);
layout.findViewById(R.id.cancel_btn).setBackgroundResource(
R.drawable.single_btn_select);
}
// set the cancel button
if (cancel_btnText != null) {
((Button) layout.findViewById(R.id.cancel_btn))
.setText(cancel_btnText);
if (cancel_btnClickListener != null) {
((Button) layout.findViewById(R.id.cancel_btn))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
cancel_btnClickListener.onClick(dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
} else {
((Button) layout.findViewById(R.id.cancel_btn))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
} else {
// if no cancel button just set the visibility to GONE
layout.findViewById(R.id.cancel_btn).setVisibility(View.GONE);
layout.findViewById(R.id.second_line).setVisibility(View.GONE);
layout.findViewById(R.id.confirm_btn).setBackgroundResource(
R.drawable.single_btn_select);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(R.id.message)).setText(message);
} else if (contentView != null) {
// if no message set
// add the contentView to the dialog body
((LinearLayout) layout.findViewById(R.id.message))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.message)).addView(
contentView, new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}
MainActivity:
public class MainActivity extends Activity implements OnClickListener{
private Button single_uialertview;
private Button single_alertdialog;
private Button double_uialertview;
private Button double_alertdialog;
private Button three_uialertview;
private Button three_alertdialog;
private CustomDialog.Builder ibuilder;
private Builder abuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
single_uialertview = (Button) findViewById(R.id.single_uialertview);
single_alertdialog = (Button) findViewById(R.id.single_alertdialog);
double_uialertview = (Button) findViewById(R.id.double_uialertview);
double_alertdialog = (Button) findViewById(R.id.double_alertdialog);
three_uialertview = (Button) findViewById(R.id.three_uialertview);
three_alertdialog = (Button) findViewById(R.id.three_alertdialog);
single_uialertview.setOnClickListener(this);
single_alertdialog.setOnClickListener(this);
double_uialertview.setOnClickListener(this);
double_alertdialog.setOnClickListener(this);
three_uialertview.setOnClickListener(this);
three_alertdialog.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.single_uialertview:
ibuilder = new CustomDialog.Builder(MainActivity.this);
ibuilder.setTitle(R.string.prompt);
ibuilder.setMessage(R.string.exit_app);
ibuilder.setPositiveButton(R.string.confirm, null);
ibuilder.create().show();
break;
case R.id.single_alertdialog:
abuilder = new Builder(MainActivity.this);
abuilder.setTitle(R.string.prompt);
abuilder.setMessage(R.string.exit_app);
abuilder.setPositiveButton(R.string.confirm, null);
abuilder.create().show();
break;
case R.id.double_uialertview:
ibuilder = new CustomDialog.Builder(MainActivity.this);
ibuilder.setTitle(R.string.prompt);
ibuilder.setMessage(R.string.exit_app);
ibuilder.setPositiveButton(R.string.confirm, null);
ibuilder.setNegativeButton(R.string.cancel, null);
ibuilder.create().show();
break;
case R.id.double_alertdialog:
abuilder = new Builder(MainActivity.this);
abuilder.setTitle(R.string.prompt);
abuilder.setMessage(R.string.exit_app);
abuilder.setPositiveButton(R.string.confirm, null);
abuilder.setNegativeButton(R.string.cancel, null);
abuilder.create().show();
break;
case R.id.three_uialertview:
ibuilder = new CustomDialog.Builder(MainActivity.this);
ibuilder.setTitle(R.string.prompt);
ibuilder.setMessage(R.string.exit_app);
ibuilder.setPositiveButton(R.string.confirm, null);
ibuilder.setNegativeButton(R.string.cancel, null);
ibuilder.setNeutralButton(R.string.dismiss, null);
ibuilder.create().show();
break;
case R.id.three_alertdialog:
abuilder = new Builder(MainActivity.this);
abuilder.setTitle(R.string.prompt);
abuilder.setMessage(R.string.exit_app);
abuilder.setPositiveButton(R.string.confirm, null);
abuilder.setNegativeButton(R.string.cancel, null);
abuilder.setNeutralButton(R.string.dismiss, null);
abuilder.create().show();
break;
default:
break;
}
}
}
h 就这样,时间原因,代码堆砌。