简体   繁体   English

按下 MainActivity 中的按钮后,SecondActivity 中的 TextView 变得可见?

[英]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.基本上,有几种方法可以做到这一点。

  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.使用意图传递数据当然,您传递了 boolean 类型或您想要的任何内容,我认为这是您在这里尝试采用的方法。 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.在你的第二个活动中,你需要重写onCreate来解析你的意图来决定你的文本是否要显示。

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. 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.

相关问题 如何将MainActivity中的值添加到SecondActivity中的列表中 - How to add values from MainActivity to list in SecondActivity 将ArrayList从SecondActivity传递到Android MainActivity - Passing ArrayList from SecondActivity to MainActivity Android 如何在Android Studio上将字符串从SecondActivity传递给MainActivity? - How do I pass string from SecondActivity to MainActivity on Android Studio? 如何从SecondActivity调用MainActivity中的非静态方法? - How to call a non-static method in MainActivity from SecondActivity? 从SecondActivity接收MainActivity中的字符串值,然后将其向下计数 - Receiving a string value in in MainActivity from SecondActivity, and then counting it down 如何从secondActivity获取开关按钮的值? - how to get value of switch button from secondActivity? 目的问​​题:如何使用另一个“适配器”类中的MainActivity将数据传递给SecondActivity - Intent problem: how to use MainActivity from another class “Adapter” to pass data to SecondActivity 如何在按钮可见时剪切文本视图的一部分 - how to cut a part TextView when a button become visible 从MainActivity更新片段内的textview - Update textview inside fragment from MainActivity 从扩展Service的类更新MainActivity的textview - Update textview of MainActivity from class that extends Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM