如何设置滚动视图内包含两个 TextView 的线性布局的可见性 GONE?

How to set visibility GONE of a Linear Layout which is inside a scroll view and contains two TextViews inside?

主布局是一个线性布局,其中有一个包含子布局的滚动视图。这是我的布局[省略了除特定布局(标有红色)之外的所有内容,因为它会很长]:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="10dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="8dp"
            android:foreground="?android:attr/selectableItemBackground"
            app:cardCornerRadius="8dp"
            app:cardElevation="10dp">

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


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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="start"
                        android:text="Income Tax:"
                        android:textColor="@color/black"
                        android:textSize="16sp" />

                    <TextView
                        android:id="@+id/tvIncomeTax"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="end"
                        android:text="0"
                        android:textColor="@color/black"
                        android:textSize="16sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </LinearLayout>

        </androidx.cardview.widget.CardView>

    </LinearLayout>

</ScrollView>

这是我的代码(删除了不必要的代码):

public class ViewSalary 扩展 AppCompatActivity {

private Spinner selectShift, selectYear, selectMonth;
private EditText edtEmployeeCode;
private Button viewSalaryBtn;
private String shift, year, month;


DatabaseReference rootDatabaseRef;

private LinearLayout layoutIncomeTax;

private TextView tvIncomeTax;

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


    viewSalaryBtn = findViewById(R.id.viewSalaryBtn);

    layoutIncomeTax = findViewById(R.id.layoutIncomeTax);

    tvIncomeTax = findViewById(R.id.tvIncomeTax);

    rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Salary");

    viewSalaryBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            viewSalary();

            String checkIncomeTax = tvIncomeTax.getText().toString();
            if (checkIncomeTax.equals("0.0")) {
                layoutIncomeTax.setVisibility(layoutIncomeTax.GONE);
            }

        }
    });


}

private void viewSalary() {

    final String empCode = edtEmployeeCode.getText().toString();

    DatabaseReference empRef = rootDatabaseRef.child(shift).child(year).child(month).child(empCode);

    empRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                String incomeTax = dataSnapshot.child("IncomeTax").getValue(String.class);

                tvIncomeTax.setText(incomeTax);

            } else {
                Toast.makeText(ViewSalary.this, "Data does not exist!", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(ViewSalary.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

}

}

我想在加载数据后,如果 TextView 值为“0.0”(如屏幕截图中标有红色的那个),则在单击按钮时隐藏所有线性布局

Screenshot

试试这个。

//use trim method to remove spaces in string 
    String checkIncomeTax = tvIncomeTax.getText().toString().trim();
        //hope you are not getting exactly 0.0.use log.i to see value then call if
                   log.i("checkIncomeTax",checkIncomeTax);
                    if (checkIncomeTax.equals("0.0")) {
                        layoutIncomeTax.setVisibility(layoutIncomeTax.GONE);
                    }
                        }

在侦听器内部执行检查。

private Spinner selectShift, selectYear, selectMonth;
private EditText edtEmployeeCode;
private Button viewSalaryBtn;
private String shift, year, month;


DatabaseReference rootDatabaseRef;

private LinearLayout layoutIncomeTax;

private TextView tvIncomeTax;

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


    viewSalaryBtn = findViewById(R.id.viewSalaryBtn);

    layoutIncomeTax = findViewById(R.id.layoutIncomeTax);

    tvIncomeTax = findViewById(R.id.tvIncomeTax);

    rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Salary");

    viewSalaryBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            viewSalary();
            // Do not check here
        }
    });


}

private void viewSalary() {

    final String empCode = edtEmployeeCode.getText().toString();

    DatabaseReference empRef = rootDatabaseRef.child(shift).child(year).child(month).child(empCode);

    empRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                String incomeTax = dataSnapshot.child("IncomeTax").getValue(String.class);

                tvIncomeTax.setText(incomeTax);
                
                // Check here
                if (incomeTax.equals("0.0")) {
                    layoutIncomeTax.setVisibility(layoutIncomeTax.GONE);
                }
                
            } else {
                Toast.makeText(ViewSalary.this, "Data does not exist!", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(ViewSalary.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

}