Как получить / поймать событие в Android
Друзья, доброго времени суток. Есть небольшой вопрос к android комьюнити Создал кастомную вьюшку и хочу определить для нее событие, а именно действие при нажатии на вью
В самой вью я переопределил метод onTouchEvent
override fun onTouchEvent(event: MotionEvent?): Boolean {
val flag = when (direction) {
"left" -> isBelowTriangle(Point(event?.x,event?.y),
Point(0f,height.toFloat()),
Point(width.toFloat(),(height/ 1.3).toFloat()),
Point(width.toFloat(), height.toFloat()))
"right" -> isBelowTriangle(Point(event?.x,event?.y),
Point(width.toFloat(), height.toFloat()),
Point(0f,(height/ 1.3).toFloat()),
Point(0f, height.toFloat()))
else -> TODO()
}
return flag
}
Activity в которой используется данное view :
class RegistrationActivity : AppCompatActivity() {
lateinit var employerSide : View
lateinit var guestSide : View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_registration)
employerSide = findViewById(R.id.employerSide)
guestSide = findViewById(R.id.guestSide)
}}
Не понимаю, как при нажатии на view поймать событие(объект MotionEvent?) и условно передать его в метод
Код layout(элементы перекрывают друг друга) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_weight="2">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/left_registration"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/mercedes_office_employer_reg"
android:scaleType="centerCrop"
android:paddingStart="70dp"
android:alpha="0.5"
>
</ImageView>
<custom_view.RegistrationCustomView
android:id="@+id/employerSide"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:direction="left"
app:color="@color/black_registration"
app:line_color="@color/white_thighs_of_a_frightened_nymph"
>
</custom_view.RegistrationCustomView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/saimaaifasanslight"
android:text="@string/employer"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:paddingTop="180dp"
android:textColor="@color/white_thighs_of_a_frightened_nymph"
>
</TextView>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/right_registration"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/dealership_mercedes_user_reg"
android:scaleType="centerCrop"
android:alpha="0.5"
>
</ImageView>
<custom_view.RegistrationCustomView
android:id="@+id/guestSide"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:direction="right"
app:color="@color/black_registration"
app:line_color="@color/white_thighs_of_a_frightened_nymph"
>
</custom_view.RegistrationCustomView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/saimaaifasanslight"
android:text="@string/guest"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:paddingTop="240dp"
android:textColor="@color/black_amber"
/>
</FrameLayout>
</LinearLayout>