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

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

How to check invisible checkbox in android studio

问题

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

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

XML代码

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:layout_marginTop="10dp"
  5. android:layout_marginBottom="5dp"
  6. android:orientation="horizontal">
  7. <TextView
  8. android:id="@+id/tv_diabetes"
  9. android:layout_width="0dp"
  10. android:layout_height="wrap_content"
  11. android:gravity="center_vertical"
  12. android:layout_gravity="center_vertical"
  13. android:fontFamily="@font/ubuntu_regular"
  14. android:layout_weight="0.2"
  15. android:text="@string/diabetes"
  16. android:textColor="@color/textColorAsh"
  17. android:textSize="16sp" />
  18. <CheckBox
  19. android:id="@+id/check_diabetes"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:theme="@style/checkBoxStyle"
  23. android:visibility="invisible" />
  24. </LinearLayout>

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

  1. case R.id.check_diabetes:
  2. if (b == true) {
  3. tv_diabetes.setTextColor(getResources().getColor(R.color.textColorBlue));
  4. check_diabetes.setVisibility(View.VISIBLE);
  5. diabetes = "1";
  6. } 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

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:layout_marginTop="10dp"
  5. android:layout_marginBottom="5dp"
  6. android:orientation="horizontal">
  7. <TextView
  8. android:id="@+id/tv_diabetes"
  9. android:layout_width="0dp"
  10. android:layout_height="wrap_content"
  11. android:gravity="center_vertical"
  12. android:layout_gravity="center_vertical"
  13. android:fontFamily="@font/ubuntu_regular"
  14. android:layout_weight="0.2"
  15. android:text="@string/diabetes"
  16. android:textColor="@color/textColorAsh"
  17. android:textSize="16sp" />
  18. <CheckBox
  19. android:id="@+id/check_diabetes"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:theme="@style/checkBoxStyle"
  23. android:visibility="invisible" />
  24. </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.

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

答案1

得分: 1

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

  1. check_diabetes.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. check_diabetes.setVisibility(View.VISIBLE);
  5. check_diabetes.setChecked(true);
  6. }
  7. });
英文:

You could try adding an onClickListener like this:

  1. check_diabetes.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. check_diabetes.setVisibility(View.VISIBLE);
  5. check_diabetes.setChecked(true);
  6. }
  7. });

答案2

得分: 0

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

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

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

  1. final CheckBox checkDiabetes= new CheckBox(R.id.check_diabetes);
  2. checkDiabetes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  3. @Override
  4. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  5. if (isChecked)
  6. checkBox.setAlpha(1f);
  7. else
  8. checkBox.setAlpha(0f);
  9. }
  10. });

答案3

得分: 0

只需使用onCheckedChangeListener

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

Simply, use onCheckedChangeListener

  1. checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
  2. if(isChecked) {
  3. 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:

确定