android 日历备忘录代码
时间: 2023-10-25 18:10:15 浏览: 130
android日历代码
以下是一个简单的 Android 日历备忘录的代码示例:
1. 在布局文件中添加一个日历视图和一个列表视图:
```xml
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
2. 在活动中初始化日历视图和列表视图,并将它们连接起来:
```java
public class MainActivity extends AppCompatActivity {
private CalendarView mCalendarView;
private ListView mListView;
private EventListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCalendarView = findViewById(R.id.calendarView);
mListView = findViewById(R.id.listView);
mAdapter = new EventListAdapter(this);
mListView.setAdapter(mAdapter);
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
List<Event> events = getEventsForDate(year, month, dayOfMonth);
mAdapter.setEvents(events);
}
});
}
private List<Event> getEventsForDate(int year, int month, int dayOfMonth) {
// 查询数据库或其他存储位置以获取给定日期的事件列表
// 这里返回一个假的事件列表
List<Event> events = new ArrayList<>();
events.add(new Event("Meeting with client", "10:00 AM", "12:00 PM"));
events.add(new Event("Lunch with coworkers", "12:30 PM", "1:30 PM"));
events.add(new Event("Team meeting", "2:00 PM", "3:00 PM"));
return events;
}
}
```
3. 创建一个事件类,以存储事件的详细信息:
```java
public class Event {
private String mName;
private String mStartTime;
private String mEndTime;
public Event(String name, String startTime, String endTime) {
mName = name;
mStartTime = startTime;
mEndTime = endTime;
}
public String getName() {
return mName;
}
public String getStartTime() {
return mStartTime;
}
public String getEndTime() {
return mEndTime;
}
}
```
4. 创建一个事件列表适配器,以在列表视图中显示事件:
```java
public class EventListAdapter extends BaseAdapter {
private List<Event> mEvents;
private Context mContext;
public EventListAdapter(Context context) {
mEvents = new ArrayList<>();
mContext = context;
}
public void setEvents(List<Event> events) {
mEvents = events;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mEvents.size();
}
@Override
public Object getItem(int position) {
return mEvents.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.event_list_item, parent, false);
}
Event event = mEvents.get(position);
TextView nameTextView = convertView.findViewById(R.id.nameTextView);
TextView timeTextView = convertView.findViewById(R.id.timeTextView);
nameTextView.setText(event.getName());
timeTextView.setText(event.getStartTime() + " - " + event.getEndTime());
return convertView;
}
}
```
5. 创建一个事件列表项布局文件,以定义每个事件在列表视图中的外观:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/timeTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
```
这样,您就可以在 Android 应用中创建一个简单的日历备忘录了。
阅读全文