Android
 Computer >> コンピューター >  >> プログラミング >> Android

AndroidアプリでCalendarViewクラスを使ってカレンダーウィジェットを実装する方法

このチュートリアルでは、AndroidアプリでCalendarViewクラスを使用してカレンダーウィジェットを実装し、ユーザーが選択した日付を取得して表示する方法を解説します。カレンダーウィジェットを使えば、日付の選択UIを簡単に組み込むことができます。

ステップ1:新しいプロジェクトを作成する

Android Studioで新しいプロジェクトを作成します。メニューから「File」→「New Project」を選択し、必要な項目をすべて入力してプロジェクトを作成してください。

ステップ2:レイアウトファイル(activity_main.xml)を編集する

res/layout/activity_main.xmlに以下のコードを追加します。TextViewで選択された日付を表示し、CalendarViewでカレンダーを画面中央に配置しています。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <TextView
      android:id="@+id/dateView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="150dp"
      android:layout_marginTop="20dp"
      android:text="Set the Date"
      android:textColor="@android:color/background_dark"
      android:textStyle="bold" />
   <CalendarView
      android:id="@+id/calender"
      android:layout_centerInParent="true"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
   </CalendarView>
</RelativeLayout>

ステップ3:MainActivity.javaにコードを追加する

src/MainActivity.javaに以下のコードを追加します。setOnDateChangeListener()リスナーを設定することで、ユーザーがカレンダー上で日付を選択したタイミングを検知できます。

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   CalendarView calendar;
   TextView dateView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      calendar = findViewById(R.id.calender);
      dateView = findViewById(R.id.dateView);
      calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
         @Override
         public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
            String Date = dayOfMonth + "-" + (month + 1) + "-" + year;
            dateView.setText(Date);
         }
      });
   }
}
ポイント:onSelectedDayChange()の引数monthは0始まり(0=1月)であるため、表示用にはmonth + 1としています。この点はよくある落とし穴なので注意しましょう。

ステップ4:AndroidManifest.xmlを確認する

androidManifest.xmlに以下のコードが記述されていることを確認します。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
   package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリを実行する

それでは、アプリを実行してみましょう。実機のAndroidスマートフォンをパソコンに接続しているものとします。Android Studioからアプリを起動するには、プロジェクト内のアクティビティファイルを開き、ツールバーのRunアイコンをクリックします。デバイスの選択画面でお使いのモバイル端末を選択すると、端末に以下のようなデフォルト画面が表示されます。

AndroidアプリでCalendarViewクラスを使ってカレンダーウィジェットを実装する方法

カレンダー上の日付をタップすると、画面上部のTextViewに選択した日付が「日-月-年」の形式で表示されます。これで、CalendarViewクラスを使ったカレンダーウィジェットの基本的な実装は完了です。

  1. Windows 11のカレンダーアプリの使い方を徹底解説!予定管理をもっと便利に

    Windows 11には、イベントやタスク、会議などの予定を管理して、日々のスケジュールを整理するのに役立つ多機能なカレンダーアプリが標準搭載されています。アプリのショートカットはタスクバーの右端にあり、日付と時刻のアイコンをクリックするだけで、小さなウィンドウでカレンダーがポップアップ表示されます。ただし、新しいWindows 11では、このポップアップからイベントを直接追加する機能が削除されている点に注意が必要です。 Windows 11でカレンダーアプリを使う方法 カレンダーアプリはオフラインでも動作しますが、GoogleカレンダーやiCloudなどと同期するには、Microsoftアカ

  2. Windows 10カレンダーアプリの使い方と基本設定ガイド

    Windows 10では、カレンダーアプリのデザインと操作性が大きく刷新されました。中でも注目すべき変更点は、Googleカレンダーの同期と表示に対応したことです。これはWindows 8.1の同名アプリでは実現できなかった機能であり、利便性が大幅に向上しています。ここでは、新しいWindows 10カレンダーアプリの主な機能について詳しく解説します。 Windows 11をご利用の方へ:Windows 11でカレンダーアプリを使う方法は、別記事「Windows 11カレンダーアプリの使い方」をご参照ください。 Windows 10カレンダーアプリの初期設定 アプリを使用するには、まず自分の