Пропуск спецификации c дочернего элемента из дочерних элементов макета, соответствующих заданному условию - PullRequest
0 голосов
/ 28 февраля 2020

У меня есть LinearLayout, который содержит пару TextViews - все вместе они имеют поведение EditText

Есть 2 стиля TextView - один для фактического ди git и секунда для разделителя "-"

LinearLayout с представлениями:

enter image description here

Каждый раз, когда я добавляю число в EditText число отображается в соответствии с положением TextViews

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

1 2 3 4 - 5 6 7 8 -_ _ _ _

Макет:

<LinearLayout
    android:id="@+id/containerCodeDigits"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:baselineAligned="false"
    android:orientation="horizontal" />

Di git просмотр:

<TextView
        android:id="@+id/labelManualCodeItem"
        style="@style/Gotham"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="@dimen/text_24sp"
        android:layout_marginBottom="@dimen/margin_8dp"/>
<View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_gravity="bottom"
        android:background="@color/business_color_1" />

Разделитель:

<TextView
        android:id="@+id/labelManualCodeItem"
        style="@style/Gotham"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/margin_8dp"
        android:gravity="center"
        android:text="@string/manualcode_item_separator"
        android:textSize="@dimen/text_16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Код

var inputtedText: Editable = "".toEditable()
        set(value) {
            field = value
            renderCode()
        }

private val textChangedListener = object : TextWatcher {

//      every time digit is inserted renderCode() all views
        override fun afterTextChanged(s: Editable) {
            inputtedText = s.toString().toEditable()
        }
}

private fun renderCode() {
    for (i in 0 until containerCodeDigits.childCount) {

//      itemContainer can be seperator view or normal digit view
        val itemContainer = containerCodeDigits.getChildAt(i)
        val itemTextView = itemContainer.findViewById<TextView>(R.id.labelManualCodeItem)

//      if current TextView is separator then skip it, 
//      but how to insert current digit to the next TextView after Seperator view is skipped
        if (itemTextView.text.toString() == "=") {
            continue
        }
//      put text to the TextView - is only applied to normal digit views
        itemTextView.text = if (inputtedText.length > i){
            inputtedText[i].toString()
        } else {
            ""
        }
    }
}

ПРИМЕЧАНИЕ. Чтобы перейти к входу di git после разделителя, пользователь должен дважды щелкнуть, поскольку клавиша также нажата на позиции сепаратора. Таким образом, когда нажата клавиша 5, она как бы «зарезервирована» для позиции разделителя, но не отображается - что также является недопустимым поведением.

1 Ответ

1 голос
/ 28 февраля 2020
private fun renderCode() {
    val pos = 0;
    for (i in 0 until containerCodeDigits.childCount) {

//      itemContainer can be seperator view or normal digit view
        val itemContainer = containerCodeDigits.getChildAt(i)
        val itemTextView = itemContainer.findViewById<TextView>(R.id.labelManualCodeItem)

//      if current TextView is separator then skip it, 
//      but how to insert current digit to the next TextView after Seperator view is skipped
        if (itemTextView.text.toString().equals("-")) {
            continue
        }
//      put text to the TextView - is only applied to normal digit views
        itemTextView.text = if (inputtedText.length > pos){
            inputtedText[pos].toString()
            pos++
        } else {
            ""
        }
    }
}

Таким образом, только если вы фактически назначаете номер полю, следующий ди git будет использоваться для следующей метки

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...