如何在Android Studio中选中不可见的复选框

huangapple go评论150阅读模式
英文:

How to check invisible checkbox in android studio

问题

我已经创建了一个带有文本的复选框,并将其设置为不可见。目前它是未选中的,我知道复选框的位置,因为我在上面有其他的方框。我希望在我点击位置时,这个复选框被选中并可见(目前是不可见的)。

我不确定是否可能,因为我在谷歌上搜索了几个小时也没有找到相关的内容。下面是代码分享。

XML代码

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_diabetes"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:layout_gravity="center_vertical"
                android:fontFamily="@font/ubuntu_regular"
                android:layout_weight="0.2"
                android:text="@string/diabetes"
                android:textColor="@color/textColorAsh"
                android:textSize="16sp" />

            <CheckBox
                android:id="@+id/check_diabetes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:theme="@style/checkBoxStyle"
                android:visibility="invisible" />
        </LinearLayout>

以下是我尝试在程序中将其设置为可见的方式。但是,当我点击其渲染位置时,它并没有变为可见。

       case R.id.check_diabetes:
            if (b == true) {
                tv_diabetes.setTextColor(getResources().getColor(R.color.textColorBlue));
                check_diabetes.setVisibility(View.VISIBLE);
                diabetes = "1";
            } else {...
英文:

I have created a checkbox with text and set as invisible. Currently it is unchecked and I know the position of the checkbox as I have boxes above. I want the box to be checked and visible when I tap at the position ( invisible at the moment ).

I am not sure if this is possible or not as I could not find anything after hours of Googling.Sharing the codes below.

xml code

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_diabetes"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:layout_gravity="center_vertical"
                android:fontFamily="@font/ubuntu_regular"
                android:layout_weight="0.2"
                android:text="@string/diabetes"
                android:textColor="@color/textColorAsh"
                android:textSize="16sp" />

            <CheckBox
                android:id="@+id/check_diabetes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:theme="@style/checkBoxStyle"
                android:visibility="invisible" />
        </LinearLayout>

Here is how I am try to make it visible programmatically. However it is not getting visible when I tap on the place where it is rendered.

       case R.id.check_diabetes:
            if (b == true) {
                tv_diabetes.setTextColor(getResources().getColor(R.color.textColorBlue));
                check_diabetes.setVisibility(View.VISIBLE);
                diabetes = "1";
            } else {...

答案1

得分: 1

你可以尝试像这样添加一个 onClickListener:

check_diabetes.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        check_diabetes.setVisibility(View.VISIBLE);
        check_diabetes.setChecked(true);
    }
});
英文:

You could try adding an onClickListener like this:

 check_diabetes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            check_diabetes.setVisibility(View.VISIBLE);
            check_diabetes.setChecked(true);
        }
    });

答案2

得分: 0

这是一种取得你想要的方式,你可以在你的布局文件中设置android:alpha="0",但是一直将android:visibility="true"设置为真。然后在代码中:

final CheckBox checkDiabetes = new CheckBox(R.id.check_diabetes);
checkDiabetes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked)
            checkBox.setAlpha(1f);
        else
            checkBox.setAlpha(0f);
    }
});
英文:

This is a hacky way to get what you want. In your layout file, set android:alpha="0", but set android:visibility="true" all the time.
And then in code

final CheckBox checkDiabetes= new CheckBox(R.id.check_diabetes);
            checkDiabetes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked)
                checkBox.setAlpha(1f);
            else
                checkBox.setAlpha(0f);
                }
            });

答案3

得分: 0

只需使用onCheckedChangeListener

checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
  if(isChecked) {
    buttonView.setVisibility = View.VISIBLE
  }
}
英文:

Simply, use onCheckedChangeListener

checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
  if(isChecked) {
  buttonView.setVisibility = View.VISIBLE

}

huangapple
  • 本文由 发表于 2020年5月30日 15:51:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/62099429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定