简体   繁体   中英

Why does my toast not display on my emulator?

I have a very basic pqctice app working with a CalendarView. I'd like a toast to display the date when a different date than the current is called. The main activity displays fine and the view works, but the toast does not display at any point. What am I missing?

My MainActivity code:

public class MainActivity extends AppCompatActivity implements OnDateChangeListener {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

base = (CalendarView) findViewById(R.id.calendarView);
date = base.getDate();

base.setOnDateChangeListener(new OnDateChangeListener() {
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        if(base.getDate() != date) {
            date = base.getDate();
            Toast.makeText(view.getContext(), month + "/" + dayOfMonth + "/" + year, Toast.LENGTH_LONG).show();
        }
    }
});

}

The XML:

<RelativeLayout
    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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.practice.andrea.whysoobsessedwithcalendars.MainActivity">


    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="com.practice.andrea.whysoobsessedwithcalendars.MainActivity"
        >

        <CalendarView
            android:layout_width="250dp"
            android:layout_height="250dp"
            android:id="@+id/calendarView"
            android:layout_centerVertical="true"
            android:layout_alignRight="@+id/textView"
            android:layout_alignEnd="@+id/textView"/>

    </FrameLayout>

</RelativeLayout>

Again should be really basic stuff. I''m not sure why it's not working. Any ideas?

您的吐司没有问题,我尝试过您的代码,问题是if块,它永远不会执行。如果您对if块进行注释,那么您的吐司就会运行。只需更改与日期更改相关的逻辑即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM