android的时间选择器和弹窗

本文最后更新于 2022年10月19日 上午

时间选择器和弹窗的示例,不知道是否规范,瞎逼写的,只能确定能用

时间选择器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public void GetDateTime() {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

DatePickerDialog picker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
String date = year + "-" + (month + 1) + "-" + day;
Log.i(TAG, "DATE:" + date);
TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String t = String.format(" %02d:%02d", hourOfDay, minute);
Log.i("TimePickerFragment", "onTimeSet: " + t);
}
}, hour, minute, DateFormat.is24HourFormat(getActivity()));
timePickerDialog.show();
}
}, year, month, day);

picker.show();

}

带确认的消息弹窗

1
2
3
4
5
6
7
8
9
10
11
12
13
private void checkPost() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("提示");
String msg = "lalallal";
builder.setMessage(msg);
builder.setPositiveButton("确定", (dialog, which) -> {
dialog.dismiss();
});
builder.setNegativeButton("取消", (dialog, which) -> {
dialog.dismiss();
});
builder.create().show();
}

带输入框的弹窗

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void inputText(){
final EditText inputServer = new EditText(getActivity());
//设置默认值
inputServer.setText("123456");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("输入").setIcon(android.R.drawable.ic_dialog_info).setView(inputServer)
.setNegativeButton("取消", null);
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String text = inputServer.getText().toString();
Log.i(TAG, "text: " + text);
//getUserIDFromQimingID(inputServer.getText().toString());
}
});
builder.show();
}

简单提示

1
Toast.makeText(getActivity(), "长度错误:"+text.length(), Toast.LENGTH_LONG).show();

android的时间选择器和弹窗
https://blog.kala.love/posts/67862bc3/
作者
久远·卡拉
发布于
2022年4月11日
许可协议