как сделать CalendarView видимым, но недоступным для редактирования с предварительно выбранной датой

Я хочу сделать представление календаря видимым, но не редактируемым, отображаемым только пользователю с выбранной датой. Я пытался установить для кликабельного атрибута значение false, но это не сработало. и я добавил представление контейнера, затем установил для кликабельного атрибута значение false, но это также не сработало

    <CalendarView
            android:clickable="false"
            android:id="@+id/calendarView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

person Eman Essam    schedule 14.01.2020    source источник
comment
покажи свой код.   -  person Nouman Ch    schedule 14.01.2020
comment
пост отредактирован   -  person Eman Essam    schedule 15.01.2020


Ответы (2)


Возвращение true из dispatchTouchEvent(MotionEvent ev) решит вашу проблему.

Используйте этот класс:

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.CalendarView;

public class CalenderView extends CalendarView {


    public CalenderView(Context context) {
        super(context);
    }

    public CalenderView(Context context, AttributeSet attribute) {
        super(context, attribute);
    }

    public CalenderView(Context context, AttributeSet attribute, int defStyle) {
        super(context, attribute, defStyle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return true;
    }
}

Для Котлина это будет:

 import android.content.Context
 import android.util.AttributeSet
 import android.view.MotionEvent
 import android.widget.CalendarView

 class CalendarView : CalendarView {
    constructor(context: Context?) : super(context!!) {}
    constructor(context: Context?, attribute: AttributeSet?) : super(context!!, attribute) 
    {}
    constructor(context: Context?, attribute: AttributeSet?, defStyle: Int) : 
    super(context!!, attribute, defStyle) {}

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        return true
    }
  }
person Nouman Ch    schedule 16.01.2020

Я сделал обходной путь, я обернул его другим представлением, а затем управляю прослушивателем кликов для этого представления, это единственное решение, которое сработало для меня, и вот мой код:

<androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/FL"
                                                   android:layout_width="match_parent"
                                                   android:layout_height="wrap_content">

    <CalendarView android:id="@+id/calendarView"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:layout_constraintBottom_toBottomOf="parent"
                  app:layout_constraintEnd_toEndOf="parent"
                  app:layout_constraintStart_toStartOf="parent"
                  app:layout_constraintTop_toTopOf="parent" />

        <View android:id="@+id/empty"
              android:layout_width="409dp"
              android:layout_height="336dp"
              android:background="@android:color/transparent"
              android:elevation="16dp"
              app:layout_constraintBottom_toBottomOf="@id/calendarView"
              app:layout_constraintEnd_toEndOf="@id/calendarView"
              app:layout_constraintHorizontal_bias="1.0"
              app:layout_constraintStart_toStartOf="@id/calendarView"
              app:layout_constraintTop_toTopOf="@id/calendarView"
              app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

Для управления слушателями:

private fun enableCalendar() {
    empty.setOnClickListener(null)
    empty.visibility = View.GONE
}

private fun disableCalendar() {
    empty.visibility = View.VISIBLE
    empty.setOnClickListener {}
}
person Eman Essam    schedule 18.02.2021