Android 自定义popuWindow
对于项目需求中,系统的弹出框不能满足我们的需求,就只能通过自定义布局来实现自己相对应的功能,这里总结一篇关于AlertDialog的
具体操作
这里把弹出框封装了一个方法,使用的时候,只需要在对应的活动页写入自己的想实现的布局就可以了。 由于本人app中设计到离线缓存数据的问题,所以对弹出框做了有网无网的处理(Android开发中如何打开手机网络设置界面(适用于所有机型)),里面还有下拉框的功能
private void showPageDialog(DepartmentByPerBean departmentByPerBean, boolean conn) {
View inflate = LayoutInflater.from(AddQualityActivity.this).inflate(R.layout.page_dialog, null);
EditText pageName = inflate.findViewById(R.id.pageName);
EditText produceYear = inflate.findViewById(R.id.produceYear);
EditText produceMonth = inflate.findViewById(R.id.produceMonth);
EditText produceDay = inflate.findViewById(R.id.produceDay);
EditText useYear = inflate.findViewById(R.id.useYear);
EditText useMonth = inflate.findViewById(R.id.useMonth);
EditText useDay = inflate.findViewById(R.id.useDay);
Spinner depart_down = inflate.findViewById(R.id.depart_down);
Button btn_save = inflate.findViewById(R.id.btn_save);
Button btn_disagree = inflate.findViewById(R.id.btn_disagree);
final AlertDialog dialog = new AlertDialog
.Builder(AddQualityActivity.this)
.setView(inflate)
.show();
// 通过WindowManager获取
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = dm.widthPixels * 9 / 10;
params.height = dm.heightPixels * 1 / 2;
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setAttributes(params);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
//获取时间
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
Log.e("系统时间 >>>>", "showPageDialog: " + year + "----" + month + "---" + date);
produceYear.setText(year + "");
produceMonth.setText(month + "");
produceDay.setText(date + "");
useYear.setText(year + "");
useMonth.setText(month + "");
useDay.setText(date + "");
if (conn == true && flag == true) {
if (departmentByPerBean.getCode() == 200 && departmentByPerBean.getData() != null) {
// 建立数据源
ArrayList<String> mItems = new ArrayList<>();
mItems.add("请选择科室");
demItems.add("0");
//遍历取出科室
for (DepartmentByPerBean.DataBeanX.DataBean arr : departmentByPerBean.getData().getData()
) {
mItems.add(arr.getDepartmentName());
demItems.add(arr.getId() + "");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddQualityActivity.this, android.R.layout.simple_spinner_item, mItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
depart_down.setAdapter(adapter);
}
}
//禁止OnItemSelectedListener默认自动调用一次
depart_down.setSelection(0, true);
//选择科室内容
depart_down.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
num = demItems.get(pos) + "";
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO
}
});
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
String pageNumber = pageName.getText().toString();
String proYear = produceYear.getText().toString();
String proMonth = produceMonth.getText().toString();
String proDay = produceDay.getText().toString();
String produce = proYear + "-" + proMonth + "-" + proDay;
String uYear = useYear.getText().toString();
String uMonth = useMonth.getText().toString();
String uDay = useDay.getText().toString();
String using = uYear + "-" + uMonth + "-" + uDay;
Log.e("科室Id >>>>", "onClick: " + num);
if (conn == false) {
Toast.makeText(AddQualityActivity.this, "网络未连接,请检查您的网络", Toast.LENGTH_SHORT).show();
} else {
if (TextUtils.isEmpty(pageNumber)) {
Toast.makeText(AddQualityActivity.this, "请输入试纸批号!", Toast.LENGTH_SHORT).show();
return;
} else if (TextUtils.isEmpty(proYear)) {
Toast.makeText(AddQualityActivity.this, "请输入生产年份!", Toast.LENGTH_SHORT).show();
return;
} else if (TextUtils.isEmpty(proMonth)) {
Toast.makeText(AddQualityActivity.this, "请输入生产月份!", Toast.LENGTH_SHORT).show();
return;
} else if (TextUtils.isEmpty(proDay)) {
Toast.makeText(AddQualityActivity.this, "请输入生产日期!", Toast.LENGTH_SHORT).show();
return;
} else if (TextUtils.isEmpty(uYear)) {
Toast.makeText(AddQualityActivity.this, "请输入使用年份!", Toast.LENGTH_SHORT).show();
return;
} else if (TextUtils.isEmpty(uMonth)) {
Toast.makeText(AddQualityActivity.this, "请输入使用月份!", Toast.LENGTH_SHORT).show();
return;
} else if (TextUtils.isEmpty(uDay)) {
Toast.makeText(AddQualityActivity.this, "请输入使用日期!", Toast.LENGTH_SHORT).show();
return;
} else if (num.equals("-1")) {
Toast.makeText(AddQualityActivity.this, "请选择科室!", Toast.LENGTH_SHORT).show();
return;
} else if (!TextUtils.isEmpty(pageNumber) && !TextUtils.isEmpty(proYear) && !TextUtils.isEmpty(proMonth) && !TextUtils.isEmpty(proDay)
&& !TextUtils.isEmpty(uYear) && !TextUtils.isEmpty(uMonth) && !TextUtils.isEmpty(uDay) && num.equals("-1") == false) {
presenter.addArticleTry("", pageNumber, produce, AccountHelper.getDeanId() + "", num, "", using, "", AccountHelper.getUserId(), AccountHelper.getToken());
}
}
}
});
btn_disagree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
|