[英]TextView from SecondActivity become visible after a button from MainActivity is pressed?
I'm very new to Android Studio Development and I was wondering how to do this, when I click a button on MainActivity, it will direct me to secondActivity where the text become visible (Originally TextView will not be visible until the button from MainActivity is pressed)我对 Android Studio Development 非常陌生,我想知道如何做到这一点,当我单击 MainActivity 上的一个按钮时,它将引导我到 secondActivity 文本变得可见(最初 TextView 直到 MainActivity 的按钮才可见按下)
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String status = "Success!";
intent2.putExtra("Status",status);
startActivity(intent);
}
});
I want to make an if-else statement for this (on SecondActivity page) where if user straight away go to SecondActivity, it will not display any text there.我想为此(在 SecondActivity 页面上)做一个 if-else 语句,如果用户直接将 go 转到 SecondActivity,它将不会在那里显示任何文本。 But if pressed the button on MainAcitivty page, the system will go to SecondActivity with the TextView displayed.但是如果按下 MainAcitivty 页面上的按钮,系统将 go 到 SecondActivity 并显示 TextView。
Thanks!谢谢!
Basically, there are several approaches you can do that.基本上,有几种方法可以做到这一点。
button.setOnClickListener {
val intent = Intent(this@MainActivity, SecondActivity::class.java).apply {
val status = true
putExtra("Status", status)
}
startActivity(intent)
}
And in your second activity, in your need to override onCreate
to parse your intents to decide your text want to display or not.在你的第二个活动中,你需要重写onCreate
来解析你的意图来决定你的文本是否要显示。
val status = intent.extras?.getBoolean("Status")
if(status) {
hideText()
} else {
showText()
}
singleton
class to keep the status in this class, and based this singleton
class status, you may choose to hide/show your text. the other approach you can deal with it is try to create singleton
class to keep the status in this class, and based this singleton
class status, you may choose to hide/show your text. However this solution isn't the recommended way to do it.但是,此解决方案不是推荐的方法。 Because global state is bad for testing and just pollute the code.因为全局 state 不利于测试,只会污染代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.