Compare commits
4 Commits
main
...
yangwenzhi
Author | SHA1 | Date |
---|---|---|
|
bd5168322b | 2 months ago |
|
f8040d695e | 4 months ago |
|
2a7af13304 | 4 months ago |
|
531c5bbbef | 4 months ago |
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
// 导入必要的包
|
||||
import java.util.Calendar;
|
||||
|
||||
import net.micode.notes.R; // 导入项目资源
|
||||
import net.micode.notes.ui.DateTimePicker; // 导入自定义的日期时间选择器
|
||||
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener; // 导入日期时间变化的监听器
|
||||
|
||||
import android.app.AlertDialog; // 导入对话框类
|
||||
import android.content.Context; // 导入上下文类
|
||||
import android.content.DialogInterface; // 导入对话框接口
|
||||
import android.content.DialogInterface.OnClickListener; // 导入对话框按钮点击监听器
|
||||
import android.text.format.DateFormat; // 导入日期格式类
|
||||
import android.text.format.DateUtils; // 导入日期工具类
|
||||
|
||||
// 自定义的日期时间选择对话框类
|
||||
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
|
||||
|
||||
// 成员变量
|
||||
private Calendar mDate = Calendar.getInstance(); // 用于存储当前选择的日期和时间
|
||||
private boolean mIs24HourView; // 标记是否使用24小时制显示时间
|
||||
private OnDateTimeSetListener mOnDateTimeSetListener; // 日期时间设置完成后的回调接口
|
||||
private DateTimePicker mDateTimePicker; // 自定义的日期时间选择器
|
||||
|
||||
// 定义日期时间设置完成后的回调接口
|
||||
public interface OnDateTimeSetListener {
|
||||
void OnDateTimeSet(AlertDialog dialog, long date); // 当日期时间设置完成后调用
|
||||
}
|
||||
|
||||
// 构造方法,初始化对话框
|
||||
public DateTimePickerDialog(Context context, long date) {
|
||||
super(context); // 调用父类的构造方法
|
||||
mDateTimePicker = new DateTimePicker(context); // 创建日期时间选择器
|
||||
setView(mDateTimePicker); // 将日期时间选择器设置为对话框的内容
|
||||
// 设置日期时间变化的监听器
|
||||
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
|
||||
public void onDateTimeChanged(DateTimePicker view, int year, int month,
|
||||
int dayOfMonth, int hourOfDay, int minute) {
|
||||
// 更新Calendar对象的日期和时间
|
||||
mDate.set(Calendar.YEAR, year);
|
||||
mDate.set(Calendar.MONTH, month);
|
||||
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
|
||||
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
|
||||
mDate.set(Calendar.MINUTE, minute);
|
||||
// 更新对话框的标题
|
||||
updateTitle(mDate.getTimeInMillis());
|
||||
}
|
||||
});
|
||||
mDate.setTimeInMillis(date); // 设置初始日期和时间
|
||||
mDate.set(Calendar.SECOND, 0); // 将秒设置为0
|
||||
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); // 设置日期时间选择器的当前日期和时间
|
||||
// 设置对话框的确定按钮和取消按钮
|
||||
setButton(context.getString(R.string.datetime_dialog_ok), this);
|
||||
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
|
||||
// 根据系统设置设置24小时制显示
|
||||
set24HourView(DateFormat.is24HourFormat(this.getContext()));
|
||||
// 更新对话框的标题
|
||||
updateTitle(mDate.getTimeInMillis());
|
||||
}
|
||||
|
||||
// 设置是否使用24小时制显示时间
|
||||
public void set24HourView(boolean is24HourView) {
|
||||
mIs24HourView = is24HourView;
|
||||
}
|
||||
|
||||
// 设置日期时间设置完成后的回调接口
|
||||
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
|
||||
mOnDateTimeSetListener = callBack;
|
||||
}
|
||||
|
||||
// 更新对话框的标题
|
||||
private void updateTitle(long date) {
|
||||
int flag =
|
||||
DateUtils.FORMAT_SHOW_YEAR | // 显示年份
|
||||
DateUtils.FORMAT_SHOW_DATE | // 显示日期
|
||||
DateUtils.FORMAT_SHOW_TIME; // 显示时间
|
||||
// 根据是否使用24小时制设置显示格式
|
||||
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_12HOUR;
|
||||
// 设置对话框的标题
|
||||
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
|
||||
}
|
||||
|
||||
// 当点击确定按钮时调用
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
if (mOnDateTimeSetListener != null) {
|
||||
// 调用回调接口,传递对话框和选择的日期时间
|
||||
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.micode.notes.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.PopupMenu;
|
||||
import android.widget.PopupMenu.OnMenuItemClickListener;
|
||||
|
||||
import net.micode.notes.R;
|
||||
|
||||
public class DropdownMenu {
|
||||
private Button mButton;
|
||||
private PopupMenu mPopupMenu;
|
||||
private Menu mMenu;
|
||||
|
||||
public DropdownMenu(Context context, Button button, int menuId) {
|
||||
mButton = button;
|
||||
mButton.setBackgroundResource(R.drawable.dropdown_icon);
|
||||
mPopupMenu = new PopupMenu(context, mButton);
|
||||
mMenu = mPopupMenu.getMenu();
|
||||
mPopupMenu.getMenuInflater().inflate(menuId, mMenu);
|
||||
mButton.setOnClickListener(new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
mPopupMenu.show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
|
||||
if (mPopupMenu != null) {
|
||||
mPopupMenu.setOnMenuItemClickListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public MenuItem findItem(int id) {
|
||||
return mMenu.findItem(id);
|
||||
}
|
||||
|
||||
public void setTitle(CharSequence title) {
|
||||
mButton.setText(title);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue