




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中怎么实现一个IOSUIAlertView对话框
Android中怎么实现一个IOSUIAlertView对话框?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面在下将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。CustomDialog使用代码:package
com.example.iosalertview;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
com.example.iosalertview.CustomDialog.Builder;
public
class
MainActivity
extends
Activity
implements
OnClickListener{
private
Button
ios_dialog_btn,android_dialog_btn;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ios_dialog_btn
=
(Button)
findViewById(R.id.ios_dialog_btn);
android_dialog_btn
=
(Button)
findViewById(R.id.android_dialog_btn);
ios_dialog_btn.setOnClickListener(this);
android_dialog_btn.setOnClickListener(this);
}
@Override
public
void
onClick(View
v)
{
switch
(v.getId())
{
case
R.id.ios_dialog_btn:
CustomDialog.Builder
builder
=
new
Builder(MainActivity.this);
builder.setTitle(R.mpt);
builder.setMessage(R.string.exit_app);
builder.setPositiveButton(R.string.confirm,
null);
builder.setNegativeButton(R.string.cancel,
null);
builder.create().show();
break;
case
R.id.android_dialog_btn:
AlertDialog.Builder
mbuilder
=
new
AlertDialog.Builder(MainActivity.this);
mbuilder.setTitle(R.mpt);
mbuilder.setMessage(R.string.exit_app);
mbuilder.setPositiveButton(R.string.confirm,
null);
mbuilder.setNegativeButton(R.string.cancel,
null);
mbuilder.create().show();
break;
default:
break;
}
}
}自定义CustomDialog代码:package
com.example.iosalertview;
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.LinearLayout;
import
android.widget.TextView;
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
View
contentView;
//对话框中间加载的其他布局界面
/*按钮坚挺事件*/
private
DialogInterface.OnClickListener
confirm_btnClickListener;
private
DialogInterface.OnClickListener
cancel_btnClickListener;
public
Builder(Context
context)
{
this.context
=
context;
}
/*设置对话框信息*/
public
Builder
setMessage(String
message)
{
this.message
=
message;
return
this;
}
/**
*
Set
the
Dialog
message
from
resource
*
*
@param
title
*
@return
*/
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,
DialogInterface.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,
DialogInterface.OnClickListener
listener)
{
this.confirm_btnText
=
confirm_btnText;
this.confirm_btnClickListener
=
listener;
return
this;
}
/**
*
Set
the
negative
button
resource
and
it's
listener
*
*
@param
confirm_btnText
*
@return
*/
public
Builder
setNegativeButton(int
cancel_btnText,
DialogInterface.OnClickListener
listener)
{
this.cancel_btnText
=
(String)
context
.getText(cancel_btnText);
this.cancel_btnClickListener
=
listener;
return
this;
}
/**
*
Set
the
negative
button
and
it's
listener
*
*
@param
confirm_btnText
*
@return
*/
public
Builder
setNegativeButton(String
cancel_btnText,
DialogInterface.OnClickListener
listener)
{
this.cancel_btnText
=
cancel_btnText;
this.cancel_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);;
//
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
{
//
if
no
confirm
button
just
set
the
visibility
to
GONE
layout.findViewById(R.id.confirm_btn).setVisibility(
View.GONE);
}
//
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);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论