Fragment中CalendarView的使用

问题描述 投票:0回答:1

我正在创建一个应用程序,我想在其中显示日历并在下面显示每个选定日期的特定统计信息,但现在我尝试在 TextView 中显示选定的日期(以及 Toast 以确保)。

如果我启动一个空项目并在 MainActivity 中运行我的代码,它工作得很好,但是当我尝试将其适应我的 CalendarFragment 时,它不起作用。日历已显示,但选择日期后没有任何反应。

这是代码完美运行的MainActivity.java:

public class MainActivity extends AppCompatActivity {

    CalendarView calendarView;
    Calendar calendar;

    TextView textViewSelectedDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        calendarView = findViewById(R.id.calendarView);
        textViewSelectedDate = findViewById(R.id.textViewSelectedDate);
        calendar = Calendar.getInstance();


        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int day) {

                Toast.makeText(MainActivity.this, day + "/" + (month + 1) + "/" + year,Toast.LENGTH_SHORT).show();

                String selectedDate = day + "/" + (month + 1) + "/" + year;
                textViewSelectedDate.setText("Selected Date: " + selectedDate);
            }
        });
    }
}

这是我的日历片段:

public class CalendarFragment extends Fragment {

    CalendarView calendarView;
    Calendar calendar;
    TextView textViewSelectedDate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_calendar, container, false);

        calendarView = view.findViewById(R.id.calendarView);
        textViewSelectedDate = view.findViewById(R.id.textViewSelectedDate);
        calendar = Calendar.getInstance();

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int day) {

                Toast.makeText(requireContext(), day + "/" + (month + 1) + "/" + year, Toast.LENGTH_SHORT).show();

                String selectedDate = day + "/" + (month + 1) + "/" + year;
                textViewSelectedDate.setText("Selected Date: " + selectedDate);
            }
        });

        return view;
    }
}

还有我的fragment_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CalendarFragment">

    <CalendarView
        android:id="@+id/calendarView"
        android:layout_width="fill_parent"
        android:layout_height="400dp"
        android:background="@color/black"
        android:dateTextAppearance="@style/CalenderViewDateCustomText"
        android:theme="@style/CalenderViewCustom"
        android:weekDayTextAppearance="@style/CalenderViewWeekCustomText" />

    <!-- TextView to display the selected date -->
    <TextView
        android:id="@+id/textViewSelectedDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Date: "
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="450dp"
        android:background="@color/black"
        android:textColor="@color/white"
       />
</FrameLayout>

我尝试了导入、stackoverflow 和 chatGPT 中的其他问题,但没有成功。如果有人能给我一些见解,我将不胜感激!

android fragment toast calendarview
1个回答
0
投票

突然就可以了,不过在另一台电脑上。

© www.soinside.com 2019 - 2024. All rights reserved.