gpt4 book ai didi

java - Android Studio 默认值/传递变量

转载 作者:行者123 更新时间:2023-12-02 10:50:24 25 4
gpt4 key购买 nike

我是 Android Studio 和 java 的新手,所以希望你能帮助我。我想将一个双变量从一个 Activity 传递到下一个 Activity 。但我不确定需要什么,因此请输入接收 Activity 中的 defaultValue。

这是 Activity 一的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button weiter = (Button)findViewById(R.id.weiter);

weiter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

EditText EingabeBreite = (EditText)findViewById(R.id.breite);

double breite = Double.parseDouble(EingabeBreite.getText().toString());

Intent rüber = new Intent(getApplicationContext(), Main2Activity.class);
getIntent().putExtra("next", breite);
startActivity(rüber);

这是第二个 Activity 的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

TextView ergebnis = (TextView)findViewById(R.id.textView2);

Bundle extras = getIntent().getExtras();
double breite = extras.getDouble("next");


ergebnis.setText(Double.toString(breite));

最佳答案

            Intent weiter  = new Intent(MainActivity.this,Main2Activity.class);
startActivity(weiter);

EditText EingabeBreite = (EditText)findViewById(R.id.breite);

double breite = Double.parseDouble(EingabeBreite.getText().toString());

Intent rüber = new Intent(getApplicationContext(),MainActivity.class);
getIntent().putExtra("next","breite");
startActivity(rüber);

首先 - 您正在启动 2 个 Intent 。那不是你想要的。只有一项 Activity 可以排在最前面,您想做其中一项。两者都不是。

其次 - 您不需要 getIntent().putExtra()。您需要ruber.putExtra()。您需要将额外的内容放在发送给其他 Activity 的 Intent 上。调用 getIntent 将获取启动当前 Activity 的 Intent,这不是您要发送给下一个 Activity 的 Intent。

关于java - Android Studio 默认值/传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52228058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com