简体   繁体   English

根据if语句将TextView设置为从不可见可见

[英]Setting a TextView to visible from invisible based on an if statement

I have several strings of text on a screen that are set to invisible when the application starts. 我在屏幕上有几个字符串,在应用程序启动时设置为不可见。 When a button is clicked on another screen, I want a specific string to become visible. 当在另一个屏幕上单击按钮时,我希望特定的字符串变得可见。 Ultimately I want to have a few strings, of the several, become visible as a result of clicking this button. 最终,我希望通过单击此按钮可以使其中的一些字符串变得可见。

    public void buttona0Click(View view){
    setContentView(R.layout.report_screen);
    buttonClicked2 = 1;
    if(buttonClicked1==1){
        setVisibility(R.id.textView2.VISIBLE);
        }
    }

I am primarily looking for guidance on this line 我主要是在这方面寻找指导

    setVisibility(R.id.textView2.VISIBLE);

I am new to programming in general, so I don't know if what I've said makes sense to most of you. 我一般对编程都不熟悉,所以我不知道我所说的内容对大多数人是否有意义。 Is .setText an alternative? .setText是否可以替代?

Write your own algo: 编写自己的算法:

  1. Use one boolean 使用一个布尔值
  2. view VISIBLE and GONE based own above boolean variable 查看基于VISIBLE和GONE的上述布尔变量

You need to instantiate your TextView first. 您需要首先实例化TextView So the easiest way to begin is to declare them before onCreate() and outside of any other method so they are member variables 因此,最简单的开始方法是在onCreate()之前和其他任何方法之外声明它们,以便它们成为成员变量

public class MyActivity extends Activity
{
    TextView tv1, tv2, etc...;

    public void onCreate(...)
    {
        super.onCreate(...);
        setContentView(R.layout.my_layout);
        tv1 = (TextView) findViewById(R.id.textView1);
        ...
     }

Then in your onClick() change the Visibility which takes an int value 然后在您的onClick()更改采用int值的Visibility

 public void buttona0Click(View view){
     buttonClicked2 = 1;
     if(buttonClicked1==1){
         tv1.setVisibility(View.VISIBLE);
    }
}

Note: please don't include the "..." above in your code. 注意:请不要在代码中包含上面的“ ...”。 That is just omitted code that I assume you know how to handle already. 那只是被忽略的代码,我假设您已经知道如何处理。 Also, I took out setContentView() from the onClick() method because typically this should only be done once in onCreate() . 另外,我从onClick()方法中取出了setContentView() ,因为通常这只能在onCreate()完成一次。

I'm not sure about the logic inside there because I don't know what the buttonClicked1 variables are for but that is how to do the Visibility . 我不确定其中的逻辑,因为我不知道buttonClicked1变量是做什么用的,但这是如何实现Visibility

setVisibility() Docs setVisibility()文件

Is .setText an alternative? .setText是否可以替代?

This will simply set the text so if you have it already set in your xml then you don't need to...you can just change the Visibility as you are trying to do. 这将简单地设置文本,因此,如果您已经在xml中设置了文本,则不需要...只需在尝试进行操作时更改Visibility即可。 If you haven't set it then you will need to with something like 如果您尚未设置,则需要使用类似

tv1.setText("Hello World");  // input your own String or String resource

Make sure you call setVisibility(View.GONE) for all your TextViews in onCreate() after setContentView() . 确保在setContentView()之后在onCreate()为所有TextView调用setVisibility(View.GONE) setContentView() Then in your if clause set the visibility of selected textview to View.VISIBLE - textview.setVisibility(View.VISIBLE) 然后在您的if子句中将所选textview的可见性设置为View.VISIBLE- textview.setVisibility(View.VISIBLE)

First findViewById of the TextView and in onClick function of your button set visibility of that textView as VISIBLE 首先是TextView findViewById ,然后在按钮的onClick函数中,将该textView的可见性设置为VISIBLE

TextView textView = (TextView) findViewById(R.id.textView1);


 public void buttona0Click(View view){
     buttonClicked2 = 1;
    if(buttonClicked1==1){
        textView.setVisibility(view.VISIBLE);
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM