SecondActivity中的TextView在从MainActivity中的按钮被按下后变得可见吗?

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

TextView from SecondActivity become visible after a button from MainActivity is pressed?

问题

我对Android Studio开发非常新,我想知道如何实现以下功能:当我在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);
}
});

我想在SecondActivity页面上添加一个if-else语句,如果用户直接转到SecondActivity,它将不显示任何文本。但如果在MainActivity页面上按下按钮,系统将转到SecondActivity并显示TextView。

谢谢!
英文:

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)

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. But if pressed the button on MainAcitivty page, the system will go to SecondActivity with the TextView displayed.

Thanks!

答案1

得分: 0

Sure, here are the translated parts:

  1. 使用意图传递数据
    确保您将布尔类型或任何您想要的数据传递给此意图,我认为这是您要尝试的方法。所以我可以给您一个示例:
    在您的第一个活动中,您可以这样做,

    button.setOnClickListener {
        val intent = Intent(this@MainActivity, SecondActivity::class.java).apply {
            val status = true
            putExtra("Status", status)
        }
        startActivity(intent)
    }
    

    而在您的第二个活动中,您需要覆盖 onCreate 方法以解析您的意图,以决定是否要显示文本。

    val status = intent.extras?.getBoolean("Status")
    if(status) {
      hideText()
    } else {
      showText()
    }
    
  2. 处理的另一种方法是尝试创建 singleton 类来保留此类中的状态,并根据此 singleton 类的状态选择隐藏/显示文本。但是,这种解决方案不是推荐的方式。因为全局状态对于测试来说是不好的,只会污染代码。

英文:

Basically, there are several approaches you can do that.

  1. Use intents pass data
    Sure you pass a boolean type or whatever you want into this intent, I think this is the approach you are trying to make here. So I can give you an example:
    In your first activity you can do something like this,
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.

val status = intent.extras?.getBoolean("Status")
if(status) {
  hideText()
} else {
  showText()
}
  1. 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.

huangapple
  • 本文由 发表于 2020年7月31日 15:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63187272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定