Java 在按下按钮之前使 Android TextView 不可见?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19694760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:27:43  来源:igfitidea点击:

Make Android TextView invisible until button pressed?

javaandroidandroid-layout

提问by Learning2Code

I am looking to make a TextView invisible until the button is pressed. Specifically they are the answers to the question and only visible once the button below it is pressed.

我希望在按下按钮之前使 TextView 不可见。具体来说,它们是问题的答案,并且只有在按下它下方的按钮后才可见。

What I have so far >>

到目前为止我所拥有的>>

public class AndroidAssignment2_1 extends Activity {

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

        Button next = (Button) findViewById(R.id.QButton);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();

            }
        }); 
            Button next1 = (Button) findViewById(R.id.QButton);
            next1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_2.class);
                    startActivityForResult(myIntent, 0);
                }
            }); 


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.android_assignment2_1, menu);
        return true;
    }

}

The xml for this class

这个类的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView android:id="@+id/Questions"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:text="@string/Q2"   />

<Button android:id="@+id/QButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_question"  />

<Button android:id="@+id/AButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send" />

<TextView android:id="@+id/Answers"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:hint="@string/A2" />

<Button android:id="@+id/QuitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_quit" />


</LinearLayout>

I dont think more is needed, I am just looking for the code that I would apply to "AButton" to make the TextView "Answers" only visible once clicked by user.

我认为不需要更多,我只是在寻找将应用于“AButton”的代码,以使 TextView“Answers”仅在用户单击时可见。

采纳答案by hasan

XML:

XML:

<TextView android:id="@+id/Answers"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:hint="@string/A2"
    android:visibility="invisible"/>

Code:

代码:

Button button = (Button) findViewById(R.id.AButton);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        TextView tv = AndroidAssignment2_1.this.findViewById(R.id.Answers);
        tv.setVisibility(View.VISIBLE);
    }
}); 

回答by blahdiblah

Set the Answers TextView visibility to invisible or gone by default.

默认情况下,将 Answers TextView 可见性设置为不可见或消失。

Find the AButton Button in the same way you find the other Button views.

以与查找其他 Button 视图相同的方式查找 AButton Button。

Set an onClickListener on the AButton Button like the other buttons' listeners that does two things:

在 AButton Button 上设置一个 onClickListener 就像其他按钮的侦听器一样,它可以做两件事:

Find the answers TextView in the same way you find the Button views.

以与查找 Button 视图相同的方式查找答案 TextView。

Calls setVisibilityon the the answers TextView.

调用setVisibilityTextView 的答案。

回答by Mayank Tripathi

For this thing to work, first we need to make a TextView, write our message in it and make the TextView invisible using visibilityproperty if Textview like this :

为了让这件事起作用,首先我们需要创建一个 TextView,在其中写入我们的消息,如果 Textview 像这样,则使用可见性属性使 TextView 不可见

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        **android:visibility="invisible"**/>

and then make a method in your main activity class to show invisbile test view in our Layout using setVisibility()method like this:

然后在您的主活动类中创建一个方法,使用setVisibility()方法在我们的布局中显示不可见的测试视图,如下所示:

  public void onLoveButtonClicked(View view){
        TextView textView = (TextView) findViewById(R.id.haikuTextView);
        textView.setVisibility(View.VISIBLE);

  }