AlertDialog(对话框)---简单对话框:
AlertDialog生成的对话框可以分为四个区域
图标区,标题区,内容区,按钮区
步骤:1、创建AlertDialog.Builder对象
2、调用AlertDialog.Builder的setTitle设置标题
3、调用AlertDialog.Builder的setIcon方法设置图片
4、AlertDialog.Builder的setMessage设置内容
5、调用AlertDialog.Builder的
setPositiveButton设置确定按钮,
setNegativeButton设置取消按钮
6、调用AlertDialog.Builder的show方法
简单alertDialog
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.example.android_lb_day4_ui.R;
public class AletDialogTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_alertdialog);
}
public void simple(View v) {
AlertDialog.Builder bulider = new AlertDialog.Builder(this);
bulider.setTitle( "简单对话框" ); // 设置标题
bulider.setIcon(R.drawable. ic_launcher); // 设置标题左边的图片
bulider.setMessage( "对话框测试内容\n第二行内容" ); // 设置显示的消息
bulider.setPositiveButton( "确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast. makeText(getApplicationContext(), "确定",
Toast. LENGTH_SHORT).show();
}
});
bulider. setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast. makeText(getApplicationContext(), "取消",
Toast. LENGTH_SHORT).show();
}
});
bulider.show();
}
}
// 消息对话框
public
void
simple(View v) {
AlertDialog.Builder bulider =
new
AlertDialog.Builder(
this
);
bulider.setTitle(
"简单消息对话框"
);
// 设置标题
bulider.setIcon(R.drawable.
ic_launcher
);
// 设置标题左边的图片
bulider.setMessage(
"简单消息提示框...\n第二行的消息"
);
setPositiveButton(bulider);
setNegativeButton(bulider);
bulider.show();
}
// 列表对话框
public
void
simpleList(View v) {
AlertDialog.Builder bulider =
new
AlertDialog.Builder(
this
);
bulider.setTitle(
"简单列表对话框"
);
// 设置标题
bulider.setIcon(R.drawable.
ic_launcher
);
// 设置标题左边的图片
bulider.setItems(
items, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast. makeText(getApplicationContext(), items[which],
Toast. LENGTH_SHORT).show();
}
});
setPositiveButton(bulider);
setNegativeButton(bulider);
bulider.show();
}
// 单选列表对话框
public
void
singleSelect(View v) {
AlertDialog.Builder bulider =
new
AlertDialog.Builder(
this
);
bulider.setTitle(
"单选列表对话框"
);
// 设置标题
bulider.setIcon(R.drawable.
ic_launcher
);
// 设置标题左边的图片
bulider.setSingleChoiceItems(
items
, 1,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
Toast. makeText(getApplicationContext(),
items
[which],
Toast.
LENGTH_SHORT
).show();
}
});
setPositiveButton(bulider);
setNegativeButton(bulider);
bulider.show();
}
//多选列表对话框
public
void
mulSelect(View v){
AlertDialog.Builder bulider =
new
AlertDialog.Builder(
this
);
bulider.setTitle(
"单选列表对话框"
);
// 设置标题
bulider.setIcon(R.drawable.
ic_launcher
);
// 设置标题左边的图片
bulider.setMultiChoiceItems(
items
,
new
boolean
[]{
false
},
null
);
setPositiveButton(bulider);
setNegativeButton(bulider);
bulider.show();
}
// 设置确定按钮
public
AlertDialog.Builder setPositiveButton(AlertDialog.Builder bulider) {
return
bulider.setPositiveButton(
"确定"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
//
TODO
Auto-generated method stub
Toast. makeText(getApplicationContext(),
"确定"
,
Toast.
LENGTH_SHORT
).show();
}
});
}
// 设置取消按钮
public
AlertDialog.Builder setNegativeButton(AlertDialog.Builder bulider) {
return
bulider.setNegativeButton(
"取消"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
//
TODO
Auto-generated method stub
Toast. makeText(getApplicationContext(),
"取消"
,
Toast.
LENGTH_SHORT
).show();
}
});
}
DatePickerDialog、TimePickerDialog使用:
只要通过new 关键字创建DatePickerDialog、TimePickerDialog的实例,调用他们的show()方法即可
DatePickerDialog的监听器:new DatePickerDialog.OnDateSetListener(){};
TimePickerDialog的监听器:newTimePickerDialog.OnTimeSetListener(){};
那么如何将Activity悬浮在另外一个Activity上面? 答案很简单:
只要把显示的activity设定为窗口即可
在AndroidMainFest.xml文件中配置: