前言
这段时间在研究android平台上的开源项目——StandupTimer,这是由jwood所设计的一个较为简单android应用,用于控制会议时间,类似秒表倒计时。
android的提醒
android 的提醒主要有3中方式:Toast Notification,Status Bar Notification,Dialog Notification;在Standup Timer 中使用了很多Dialog notification。特别是在删除某项时,弹出的确认对话框。Dialog Notification 主要分为四种:Alert Dialog,ProgressDialog ,DatePickerDialog ,TimePickerDialog。本文将重点讲述Alert Dialog。
Alert Dialog
Alert Dialog的创建比较简单,我们将按步骤来创建一个带有EditText view的警告对话框,通过该对话框创建Standup timer中参加会议的团队。
第一步,重写 onCreateDialog方法,根据ID创建不同的对话框。
@Override
protected
Dialog onCreateDialog(
int
id)
{
switch
(id)
{
case
CREATE_TEAM_DIALOG:
if
(createTeamDialog
==
null
)
{
AlertDialog.Builder builder
=
new
AlertDialog.Builder(
this
);
builder.setTitle(R.string.add_team);
builder.setView(getTextEntryView());
builder.setCancelable(
true
);
//
设置确定按钮和监听其事件
builder.setPositiveButton(R.string .ok, addTeamButtonListener());
//
设定否定按钮和监听其事件
builder.setNegativeButton(R.string.revert, cancelListener());
createTeamDialog
=
builder.create();
}
return
createTeamDialog;
default
:
Logger.e(
"
Attempting to create an unkonwn dialog with an id of
"
+
id);
return
null
;
}
}
AlertDialog.Builder 是用来创建具体警告对话框的。
setTitle()为设置对话框的标题;
setView(View)用于给对话框加载View,如果需要的话,这里加载的是一个EditText。
setCancelable(boolean) 设置返回键是否能撤销对话框,一般为true
setPositiveButton()设置按下表示确定按钮时按钮的text,和按钮的事件监听器
setNegativeButton()设置取消按钮的text 和监听器
另外如果对话框不需要特别的视图控件的话可以不使用setView()。直接通过 setMessage(Msg)来显示你需要的展示的警告信息。
Builder各项属性设置完成后,即可通过builder.create(),创建AlertDialog对话框。
第二步展示对话框
展示对话框非常简单,在你需要弹出提醒的操作里加入showDialog(id) 即可。通常根据id对对话框进行封装。
private
void
displayAddTeamDialog() {
showDialog(CREATE_TEAM_DIALOG);
}
第三步编写对话框中按钮事件
这与普通按钮的事件编写相似,这里按钮的事件处理的是添加一个团队操作。
private
OnClickListener addTeamButtonListener() {
return
new
DialogInterface.OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
id) {
EditText collectedTextView
=
(EditText) getTextEntryView().findViewById(R.id.collected_text);
String name
=
collectedTextView.getText().toString();
Team.create(name, TeamListActivity.
this
);
teamListAdapter.add(name);
}
};
}
返回的是 DialogInterface 下的 OnClickListener。
private
OnClickListener cancelListener() {
return
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
dialog.cancel();
}
};
}
撤销对话框操作,撤出警告。
关于setView
代码中有几处调用了getTextEntryView方法,这是一个返回View的自定义方法。setView(View) 可以让展现你想要的View,用来接收用户的输入信息等。
synchronized
protected
View getTextEntryView() {
if
(txtEntryView
==
null
) {
LayoutInflater factory
=
LayoutInflater.from(
this
);
txtEntryView
=
factory.inflate(R.layout.collect_text,
null
);
}
return
txtEntryView;
}
LayoutInflater 可以将res 文件夹下的layout 布局的XML 转化为 View。也可以通过
getLayoutInflater() 或 getSystemService(String)
方法创建LayoutInflater
LayoutInflater inflater
=
(LayoutInflater)context.getSystemService
Context.LAYOUT_INFLATER_SERVICE);
inflate(int resId,
ViewGroup root),将预先定义的XML布局文件转化为View
<?
xml version="1.0" encoding="utf-8"
?>
<
EditText
xmlns:android
="http://schemas.android.com/apk/res/android"
android:id
="@+id/collected_text"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:padding
="5dp"
/>