如何通过多个包含将 onclick 侦听器设置为线性布局中的所有项目

How to set onclick listeners to all items in linearlayout through multiple include's

我们正在构建 android 警报应用程序,并且对移动开发了解不多。所以问题是:我们有 days_of_the_week.xml 线性布局和水平星期几,includeddays_pick.xml版式,此版式包含activity_add_page.xml中。 我们希望能够以某种方式选择并突出显示用户在该组件中选择的日期。

不写这样的东西可以做到吗:

        one_week.monday_tv.setOnClickListener{
        it.setBackgroundColor(getColor(R.color.purple_active))
    }

days_of_the_week.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/week_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/monday_tv"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/monday" />

    <TextView
        android:id="@+id/tuesday_tv"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tuesday" />

    <TextView
        android:id="@+id/wednesday_tv"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/wednesday" />

    <TextView
        android:id="@+id/thursday_tv"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/thursday" />

    <TextView
        android:id="@+id/friday_tv"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/friday" />

    <TextView
        android:id="@+id/saturday_tv"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/saturday" />

    <TextView
        android:id="@+id/sunday_tv"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sunday" />

</LinearLayout>

days_pick.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/one_week"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:visibility="visible"
        >

        <include
            android:id="@+id/inc_week"
            layout="@layout/days_of_the_week"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <Button
            android:id="@+id/show_two_weeks_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/even_odd_dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/inc_week"
            app:layout_constraintVertical_bias="0.043" />
    </androidx.constraintlayout.widget.ConstraintLayout>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/two_weeks"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:visibility="gone"
        >


        <TextView
            android:id="@+id/odd_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:rotation="-90"
            android:text="@string/odd_dp"
            android:textSize="12sp"
            app:layout_constraintEnd_toStartOf="@+id/odd_week"
            app:layout_constraintHorizontal_bias="0.901"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/even_tv"
            android:layout_width="30dp"
            android:layout_height="18dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="8dp"
            android:rotation="-90"
            android:text="@string/even_dp"
            android:textSize="12sp"
            app:layout_constraintEnd_toStartOf="@+id/even_week"
            app:layout_constraintHorizontal_bias="0.984"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/odd_tv" />

        <include
            android:id="@+id/odd_week"
            layout="@layout/days_of_the_week"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <include
            android:id="@+id/even_week"
            layout="@layout/days_of_the_week"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/odd_week"
            app:layout_constraintVertical_bias="0.007" />

        <Button
            android:id="@+id/show_one_week_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:text="@string/one_week_dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/even_week"
            app:layout_constraintVertical_bias="0.0" />


    </androidx.constraintlayout.widget.ConstraintLayout>



</LinearLayout>

activity_add_page.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TimePicker
            android:id="@+id/time_picker"
            style="@style/TimePickerAddingPageTheme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="36dp"
            android:layout_marginBottom="20dp"
            android:scaleX="1.50"
            android:scaleY="1.50"
            android:theme="@style/TimePickerAddingPageTheme"
            android:timePickerMode="spinner"/>

        <include
            android:id="@+id/week_pick"
            layout="@layout/days_pick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintVertical_bias="0.212"
            android:layout_margin="20dp"/>

        <ScrollView
            android:id="@+id/scrollView2"
            android:layout_width="match_parent"
            android:layout_height="400dp">

            <LinearLayout
                android:id="@+id/linearLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <View
                    android:id="@+id/divider2"
                    android:layout_width="wrap_content"
                    android:layout_height="1dp"
                    android:layout_gravity="center"
                    android:layout_marginStart="28dp"
                    android:layout_marginEnd="28dp"
                    android:layout_marginBottom="10dp"
                    android:background="?android:attr/listDivider" />

                <EditText
                    android:id="@+id/editTextTextPersonName"
                    android:layout_width="347dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:autofillHints="username"
                    android:backgroundTint="?attr/colorError"
                    android:ems="10"
                    android:hint="@string/signal_name_ap"
                    android:inputType="textPersonName"
                    android:maxLength="15" />

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="72dp">

                    <TextView
                        style="@style/EachAddingPageTextStyle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="30dp"
                        android:text="@string/repeat_signal_ap"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintVertical_bias="0.509" />


                    <Spinner
                        android:id="@+id/repeat_spn_ap"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="28dp"
                        android:background="@drawable/spinner_background"
                        android:entries="@array/timeset_array"
                        android:gravity="center_vertical"
                        android:paddingStart="0dp"
                        android:paddingEnd="20dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                </androidx.constraintlayout.widget.ConstraintLayout>

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="72dp">

                    <TextView
                        style="@style/EachAddingPageTextStyle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="30dp"
                        android:text="@string/signal_melody_ap"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintVertical_bias="0.509" />

                    <Button
                        android:id="@+id/melody_btn_ap"
                        android:layout_width="30dp"
                        android:layout_height="28dp"
                        android:layout_marginEnd="25dp"
                        android:background="@drawable/ic_arrow_left"
                        android:onClick="onClick"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                </androidx.constraintlayout.widget.ConstraintLayout>

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="72dp">

                    <TextView
                        style="@style/EachAddingPageTextStyle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="30dp"
                        android:text="@string/vibration_ap"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintVertical_bias="0.433" />

                    <androidx.appcompat.widget.SwitchCompat
                        android:id="@+id/vibration_switch"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="25dp"
                        android:thumb="@drawable/custom_thumb"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintVertical_bias="0.511"
                        app:showText="false"
                        app:switchMinWidth="40dp"
                        app:track="@drawable/custom_track" />

                </androidx.constraintlayout.widget.ConstraintLayout>

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="72dp">

                    <TextView
                        style="@style/EachAddingPageTextStyle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="30dp"
                        android:text="@string/puzzle_on_alarm_ap"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintVertical_bias="0.509" />

                    <androidx.appcompat.widget.SwitchCompat
                        android:id="@+id/puzzle_switch"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="25dp"
                        android:thumb="@drawable/custom_thumb"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintVertical_bias="0.511"
                        app:showText="false"
                        app:switchMinWidth="40dp"
                        app:track="@drawable/custom_track" />

                </androidx.constraintlayout.widget.ConstraintLayout>



            </LinearLayout>
        </ScrollView>
    </LinearLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="72dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            style="@style/BottomBtnAddingPageTextStyle"
            android:id="@+id/cancel_tv_ap"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity=""
            android:onClick="onClick"
            android:text="@string/cancel_ap"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/space2"
            app:layout_constraintHorizontal_bias="0.506"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.509" />

        <Space
            android:id="@+id/space2"
            android:layout_width="wrap_content"
            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" />

        <TextView
            style="@style/BottomBtnAddingPageTextStyle"
            android:id="@+id/save_tv_ap"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="@string/save_ap"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.503"
            app:layout_constraintStart_toEndOf="@+id/space2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.509" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

AddPageActivity.kt:

package com.example.alarme

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.*
import androidx.appcompat.widget.SwitchCompat
import androidx.core.view.children
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import com.example.alarme.db.Alarm
import com.example.alarme.db.AlarmViewModel
import kotlinx.android.synthetic.main.activity_add_page.*
import kotlinx.android.synthetic.main.days_of_the_week.*
import kotlinx.android.synthetic.main.days_of_the_week.view.*
import kotlinx.android.synthetic.main.days_pick_fragment.*
import kotlinx.android.synthetic.main.days_pick_fragment.view.*
import java.sql.Time
import java.util.Calendar

class AddPageActivity : FragmentActivity() {
    private lateinit var alarmViewModel: AlarmViewModel
    var mTimePicker: TimePicker? = null
    var now: Calendar? = null
    var alarmName: EditText? = null
    var vibration: SwitchCompat? = null
    var puzzle: SwitchCompat? = null
    var weekMode: Spinner? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_page)
        title = ""

        mTimePicker = findViewById(R.id.time_picker)
        mTimePicker?.setIs24HourView(true)

        mTimePicker?.descendantFocusability = TimePicker.FOCUS_BLOCK_DESCENDANTS

        //TODO: after localization be made, add verification of a language -> to 24 Hour mode/to AM,PM mode

        now = Calendar.getInstance()
        alarmName = findViewById(R.id.editTextTextPersonName)
        vibration = findViewById(R.id.vibration_switch)
        puzzle = findViewById(R.id.puzzle_switch)
        weekMode = findViewById(R.id.repeat_spn_ap)
        mTimePicker?.hour = now!![Calendar.HOUR_OF_DAY]
        mTimePicker?.minute = now!![Calendar.MINUTE]

        alarmViewModel = ViewModelProvider(this).get(AlarmViewModel::class.java)


        // my garbage listeners

//        val daysOneWeek = one_week.children.forEach {
//            daysListener(one_week, it.id)
//        }

        one_week.monday_tv.setOnClickListener{
            it.setBackgroundColor(getColor(R.color.purple_active))
        }

        daysListener(one_week, R.id.monday_tv)

        show_one_week_btn.setOnClickListener{
            one_week.visibility = View.VISIBLE
            two_weeks.visibility = View.GONE
        }

        show_two_weeks_btn.setOnClickListener{
            one_week.visibility = View.GONE
            two_weeks.visibility = View.VISIBLE
        }


        repeat_spn_ap.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View?, pos: Int, id: Long) {
                if (pos == 2) {
                    week_pick.visibility = View.VISIBLE
                }
                else {
                    week_pick.visibility = View.GONE
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                week_pick.visibility = View.GONE
                // Another interface callback
            }
        }
    }

    private fun daysListener(weekCount: View, day: Int){
        weekCount.setOnClickListener{
            val currentDay = it.findViewById<TextView>(day)
            currentDay.setOnClickListener{
                currentDay.setBackgroundColor(getColor(R.color.purple_active))
            }
        }
    }

    fun onClick(view: View) {
        when (view.id) {
            R.id.melody_btn_ap -> {
                startActivity(Intent(this, MelodyPickActivity::class.java))
            }
            R.id.cancel_tv_ap -> {
                finish()
            }
            R.id.save_tv_ap -> {
                addAlarmToDb()
                finish()
            }
        }
    }

    private fun addAlarmToDb() {
        alarmViewModel.addAlarm(Alarm(
                0,
                "" + alarmName?.text,
                Time(mTimePicker!!.hour, mTimePicker!!.minute, 0),
                true,
                vibration?.isChecked!!,
                0, // will be fixed later with updated UI
                weekMode?.selectedItemPosition!!
        ))
    }
}

如您所见,我尝试使用 forEach 但没有用

您可以在 AddPageActivity.kt 中声明一个通用方法。如下所示:

 fun onClickDayOfWeek(view: View) {
    Log.d("TAG", "View Clicked having ID= " + view.id)
   view.setBackgroundColor(getColor(R.color.purple_active))
 }

然后像下面这样处理每个 view 的点击:

<TextView
    android:id="@+id/monday_tv"
    android:padding="5dp"
    android:layout_margin="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/monday"
    android:onClick="onClickDayOfWeek" /> <!-- handling click-->

<TextView
    android:id="@+id/tuesday_tv"
    android:padding="5dp"
    android:layout_margin="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tuesday"
    android:onClick="onClickDayOfWeek" /> <!-- handling click-->