1.对话框
方式一
// 1.创建AlertDialog
AlertDialog dialog = new AlertDialog.Builder(MainActivityAlert.this).create();
// 2.设置title
dialog.setTitle("提示ℹ2");
//3.设置内容
dialog.setMessage("确定吗?");
//设置确定按钮和监听函数
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//4. 显示
dialog.show();
方式二
// 1.创建 AlertDialog.Builder
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivityAlert.this);
// 2.构建对话框
builder.setTitle("提示"); //title
builder.setMessage("你确定吗?"); //内容
//3.构建确定按钮和监听汗水
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
//4.构建取消按钮
builder.setNegativeButton("取消",null);
builder.show();
2.popupwindow
// 设置PopupWindow
public void showPopupWindow(View view){
// 1.实例化对象
View v = LayoutInflater.from(this).inflate(R.layout.popup_window_xml,null);
// 参数1:用在弹出中的view // 2,3参数就是弹出框的宽高 // 4,能否获取焦点
PopupWindow popupWindow = new PopupWindow(v,600,240,true);
// 2.设置(背景,动画)
//2.1设置背景
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// 设置能相应外部的点击事件
popupWindow.setOutsideTouchable(true);
// 设置能相应点击事件
popupWindow.setTouchable(true);
// 3.显示 显示到哪里 ,参数2,3(x,y 偏移量)
popupWindow.showAsDropDown(view,50,50);
}
3.ArrayAdapter 数组适配器,前端 select
private void showArrayDialog() {
final String[] item = {"java","mysql","C","java1","mysql1","C1","java2","mysql2","C2"};
// 数组适配器
// 参数1 环境 参数2:布局资源索引(每一项资源程序的样式)参数3:数据源
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line,item);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
// adapter 显示 和点击某一项事件监听
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int index) {
// 打印当前点击的某一项
Toast.makeText(MainActivityPopup.this,item[index],Toast.LENGTH_LONG).show();
dialog.dismiss(); // 让当前对话框消失
}
});
builder.show();
}