简体   繁体   English

有什么方法可以让 Activity 在单击时对每个按钮做出不同的反应?

[英]Is there any way to make an Activity react differently for each button when clicked?

Consider you have a Button A and button B both of them when clicked it start Activity1 which contains only one TextView.假设您有一个按钮 A 和按钮 B,当您单击它时,它们都会启动 Activity1,其中仅包含一个 TextView。 Now When I click on Button A it should start Activity1 and setTextView to "The click was from A" and when clicked on B it should set the text to "The click was from B".现在,当我单击按钮 A 时,它应该启动 Activity1 并将 setTextView 设置为“点击来自 A”,当点击 B 时,它应该将文本设置为“点击来自 B”。

So I figure out that by making a global Boolean variable but I wanted to know is there any other way that is more efficient than making a global Boolean variable(the code become really messy with Boolean)所以我发现通过制作一个全局 Boolean 变量,但我想知道有没有其他方法比制作一个全局 Boolean 变量更有效(代码变得非常混乱布尔)

And this all is just an example in reality I want to add a lot of code instead of just setting the text.而这一切只是现实中的一个例子,我想添加很多代码而不是仅仅设置文本。

And pls if anyone one of you is leaving a Downvote kindly also say the reason way, this will help me frame a better question next time.如果你们中的任何一个人要离开投票,请也请说出原因,这将有助于我下次提出更好的问题。 Thank you谢谢

TL;DR TL;博士

Pass arguments with your Intent object that launches Activity2 .使用启动Activity2Intent object 传递 arguments 。 Use putExtra methods.使用putExtra方法。 That is the default way of passing small pieces of data.这是传递小块数据的默认方式。

Explained解释

"making a global Boolean variable" is a bad solution (nothing personal, it just does not fit the given problem) in this situation as anyone has access to that variable and the value can be changed at any point in time making it unreliable.在这种情况下,“制作一个全局 Boolean 变量”是一个糟糕的解决方案(不是个人的,它只是不适合给定的问题),因为任何人都可以访问该变量,并且该值可以随时更改,使其不可靠。

When you launch activity with Intent you can use putExtra methods on it ( example of such method in docs ).当您使用Intent启动活动时,您可以在其上使用putExtra方法( 文档中此类方法的示例)。 For example, there is a putExtra that accepts boolean as a value: link .例如,有一个putExtra接受boolean作为值: link Using that method you can remove the global variable, but the code still could be messy.使用该方法您可以删除全局变量,但代码仍然可能很混乱。

If this boolean variable is simply deciding which label to show you can pass the label itself using these putExtra methods.如果这个 boolean 变量只是决定要显示哪个 label,您可以使用这些putExtra方法传递 label 本身。 It would look like this:它看起来像这样:

// From Activity1 when you click Button A
buttonA.setOnClickListener {
    Intent intent = new Intent(Activity1.this, Activity2.class);
    intent.putExtra(Activity2.SOME_KEY, "This label is from buttonA.");
    startActivity(intent);
}
// From Activity1 when you click Button B
buttonB.setOnClickListener {
    Intent intent = new Intent(Activity1.this, Activity2.class);
    intent.putExtra(Activity2.SOME_KEY, "This label is from buttonB. A slightly different one.");
    startActivity(intent);
}

Activity2.SOME_KEY is some public static variable that you should declare to be sure that you use the same key for setting and getting back the value. Activity2.SOME_KEY是一些公共 static 变量,您应该声明它以确保使用相同的键来设置和取回值。 You can name it differently.你可以用不同的名字来命名它。 It must be of String type.它必须是字符串类型。 There is no need to declare it in Activity2 class but since it is the key for passing arguments for Activity2 only I think that is the most fitting place.无需在Activity2 class 中声明它,但因为它是仅通过Activity2的 arguments 的关键,我认为这是最合适的地方。

And now in you can get that value back in Activity2 :现在您可以在Activity2中取回该值:

class Activity2 extends Activity {
    public static String SOME_KEY = "some string value";

    private String labelValue = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        labelValue = getIntent().getStringExtra(SOME_KEY);
        
        // ... other stuff here like setContentView
        // use `labelValue` to set text into some TextView.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮时从“活动”移动到“片段” - Move from Activity to Fragment when button clicked 单击按钮时如何打开新活动? - How to open a new activity when a button is clicked? 单击电话后退按钮时重置活动 - Reset Activity when phones back button is clicked 单击主页按钮时如何将活动重定向到其先前的活动 - How to redirect a activity to its previous activity when home button is clicked Java:如果单击任何单选按钮,则从适配器到活动获取信息 - Java: get Information from Adapter to Activity if any radio button is clicked or not 三个按钮,每个按钮以不同的Int启动相同的Activity。 但是,当选择任何按钮时,它将加载所有3个活动 - Three buttons, each start the same Activity with a different Int. But when selecting any button, it loads all 3 Activities 单击时是否有办法使工具提示文本显示在按钮区域的随机位置? - Is there a way to make a tooltip text show in random location in the button area when clicked? 单击按钮时如何显示文本 - how to make text appear when a button is clicked 如何在Java中单击时使按钮不可见? - How to make a button invisible when clicked in java? 单击按钮时如何制作随机布局 - How to make a Random Layout when button clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM