Представления не отражают каких-либо изменений, когда это делается программно - PullRequest
0 голосов
/ 19 февраля 2020
The basic view hierarchy is this:
secondActivity
    linearLayout(LinearLayout)
        constLayout(ConstraintLayout)
            textbox(TextView)
            image(ImageView)
            image2
            image3
            ...

Текстовое поле TextView имеет видимость GONE, и цель состоит в том, чтобы сделать его ВИДИМЫМ при щелчке других видимых братьев и сестер, изменить некоторые цвета и текст, и при повторном нажатии оно должно быть снова невидимым и отменить все изменения. Не могу понять, чего бы мне не хватало. Я проверил многие старые проекты, в которых я делал то же самое, и не могу найти видимых различий в том, почему этот код не работает сейчас.

secondActivity. java

public class secondActivity extends Activity {

    public boolean isTextBoxHidden;
    public ConstraintLayout constLayout;
    public TextView textbox;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        constLayout = findViewById(R.id.constLayout);
        textbox = findViewById(R.id.textbox);
        isTextBoxHidden = false;

        // SETTING UP LISTENER
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isTextBoxHidden) {
                    constLayout.setBackgroundColor(Color.BLACK);  //setting color on previously 
                    v.setBackgroundColor(Color.BLACK);            //setting color on visible view

                    textbox.setText("whatever");
                    textbox.setVisibility(View.VISIBLE);  //was gone
                    isTextBoxHidden = true;
                }
                else {
                    textbox.setVisibility(View.GONE);     //hide again
                    constLayout.setBackgroundColor(Color.WHITE);
                    v.setBackgroundColor(Color.WHITE);
                    isTextBoxHidden = false;
                }
            }
        };

        // INSERTING LISTENERS into all children
        for(int i=0; i<constLayout.getChildCount(); i++) {
                constLayout.getChildAt(i).setOnClickListener(clickListener);
        }
    }
}

секунда действия. 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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".secondActivity">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <TextView
            android:id="@+id/textbox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="example"

            android:visibility="gone"

            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <ImageButton
            android:id="@+id/image"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/example"

            android:clickable="true"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"/>

        <!--few more clones of the first imageButton-->

    </android.support.constraint.ConstraintLayout>
</LinearLayout>

1 Ответ

0 голосов
/ 19 февраля 2020

Я не вижу, где вы устанавливаете textbox ссылку, так что, возможно, это подсказка.

Редактировать: Скомпилировал этот пример, который вы предоставили, и все работает правильно, но я предполагаю, что [...] gone again for good означало, что вы, вероятно, хотите, чтобы это было действие одним выстрелом, поэтому вместо boolean просто используйте Boolean и сравните его с нулем.

Редактировать: Со второй мысли вы можете просто удалить isTextBoxHidden = false; в другой ветке

...