gpt4 book ai didi

java - 以编程方式完成时, View 不反射(reflect)任何更改

转载 作者:行者123 更新时间:2023-12-01 18:20:53 25 4
gpt4 key购买 nike

The basic view hierarchy is this:secondActivity    linearLayout(LinearLayout)        constLayout(ConstraintLayout)            textbox(TextView)            image(ImageView)            image2            image3            ...

The textbox TextView has visibility GONE, and goal is to make it VISIBLE on clicking other visible siblings, change some colors and text, and when clicked again it must be invisible again and reverse all changes.Cant understand whatever it is that am missing. I have checked many older projects in which I did the same thing and cant find any visible differences as to why this code is not working now.

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);
}
}
}

activity_second.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>

最佳答案

我看不到您在哪里设置文本框引用,所以也许这是一个线索。

编辑:是否编译了您提供的示例并且一切正常,但我认为 [...] 再次消失 意味着您可能希望这是一次操作,所以而不是 boolean 只需使用 Boolean 并将其与 null 进行比较。

编辑:再一想,您可以在 else 分支中删除 isTextBoxHidden = false;

关于java - 以编程方式完成时, View 不反射(reflect)任何更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60292678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com