[英]How to make text appear after clicking a button
I want to appear a text (or button, whatever) after a button is clicked: 我想在单击按钮后显示一个文本(或按钮,无论如何):
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
- Now what? - 怎么办? Create an intent? 创建意图?
Let's say there's a textView that I want to have shown after the button5 is clicked. 假设有一个我想在单击button5之后显示的textView。 How-to? 如何?
You will just do whatever you need to do to your TextView inside your onClick event, an example of how this may look: 您将在onClick事件中对TextView做任何您需要做的事情,此示例可能是这样的:
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView tv = findViewById(R.id.textViewInLayout);
//Change visibility
tv.setVisibility(View.VISIBLE);
//set a value in the textview
tv.setText("Hello World");
}
};
In your method onClick(View v) add a line: 在您的方法onClick(View v)中添加一行:
myTextView.setVisibility(View.VISIBLE)
Where myTextView is the view containing the text you want to show. 其中myTextView是包含要显示的文本的视图。
Let's say there's a textView that I want to have shown after the button5 is clicked. 假设有一个我想在单击button5之后显示的textView。 How-to? 如何?
Define your TextView
in XML as usual, but set its visibility to either invisible
or gone
: 照常使用XML定义TextView
,但将其可见性设置为invisible
或gone
:
<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
( visibility="invisible"
means the TextView
will take up space in the layout, as though it were there, but won't actually appear. visibility="gone"
means the TextView
will not take up space in the layout at all, as though it were not there.) ( visibility="invisible"
表示TextView
将占用布局中的空间,就好像它在那里一样,但实际上不会出现。 visibility="gone"
表示TextView
根本不会占用布局中的空间,因为虽然不在那里。)
Then, in your OnClickListener
: 然后,在您的OnClickListener
:
textView1.setVisibility(View.VISIBLE);
And the TextView
will appear. 然后将出现TextView
。
Alternatively, you can have the TextView
be visible all along but with no text, and then call setText()
in your listener. 或者,可以使TextView
一直可见,但没有文本,然后在侦听器中调用setText()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.