[英]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
.使用启动
Activity2
的Intent
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.