Android 选项卡将意图附加信息传递给新选项卡活动

发布于 2024-10-09 00:16:31 字数 204 浏览 0 评论 0原文

我已阅读过类似的问题,但没有看到这样的问题。我有一个简单的计算器应用程序,有两个选项卡。每个人都有自己的活动课程。我最初是在第一个屏幕上使用一个按钮编写的,该按钮会接受输入并将其传递到结果屏幕,该屏幕将进行一些计算,然后显示结果。现在我想用 TabHost 来做到这一点。我已设置好两个屏幕,但不知道如何获取输入并将其传递给结果活动以进行计算并显示结果值。

提前致谢 迪安-O

I have read through the similar questions but do not see one like this. I have a simple calculator application there are two tabs. Each has their own activity class. I initially wrote this with a button on the first screen which onClick would take the inputs and pass them to the results screen which would do some calculation and then display the results. Now I want to do it with a TabHost. I have the two screens all set, but no idea how to take the inputs and pass them to the results activity to do the calculations and display the resulting values.

Thanks in advance
Dean-O

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

豆芽 2024-10-16 00:16:32

最自然的方法是提供您自己的 android.app.Application 子类并使用它来存储共享数据。然后第一个选项卡将设置数据结构中的值,第二个选项卡将读取它们并使用它们来执行您想要执行的任何计算。请参阅此处:如何在 Android 中声明全局变量?

假设您不这样做想要采用这种方法并且确实想使用 Intent extras 在 TabHost 内的 Activity 之间传递数据,您可以执行类似以下操作的操作,其中使用 TabHosts Intent(通过 getParent().getIntent() 访问)来传递数据来回。

 public class Tab1 extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tab_one);
  Button button = (Button) findViewById(R.id.btn);
  button.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    EditText x = (EditText) findViewById(R.id.x);
    EditText y = (EditText) findViewById(R.id.y);
    int a = Integer.parseInt(x.getText().toString());
    int b = Integer.parseInt(y.getText().toString());
    Intent i = getParent().getIntent();
    i.putExtra("a", a);
    i.putExtra("b", b);
    i.putExtra("tab", 1);
    TabActivity ta = (TabActivity) Tab1.this.getParent();
    ta.getTabHost().setCurrentTab(1);
   }
  });
 }
}


public class Tab2 extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView result = new TextView(this);
  Intent i = getParent().getIntent();
  int a = i.getIntExtra("a", 0);
  int b = i.getIntExtra("b", 0);
  int sum = a + b;
  result.setText(Integer.toString(sum));
        setContentView(result);
 }
}

The most natural way to do this would be to provide your own subclass of android.app.Application and use it to store the shared data. Then the first tab would set the values in the data structure, and the second tab would read them and use them to perform whatever calculation you wanted to do. See here: How to declare global variables in Android?

Assuming you don't want to take this approach and really want to use Intent extras to pass the data between Activities within a TabHost, you could do something like the following hack where you use the TabHosts Intent (accessed via getParent().getIntent()) to pass data back and forth.

 public class Tab1 extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tab_one);
  Button button = (Button) findViewById(R.id.btn);
  button.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    EditText x = (EditText) findViewById(R.id.x);
    EditText y = (EditText) findViewById(R.id.y);
    int a = Integer.parseInt(x.getText().toString());
    int b = Integer.parseInt(y.getText().toString());
    Intent i = getParent().getIntent();
    i.putExtra("a", a);
    i.putExtra("b", b);
    i.putExtra("tab", 1);
    TabActivity ta = (TabActivity) Tab1.this.getParent();
    ta.getTabHost().setCurrentTab(1);
   }
  });
 }
}


public class Tab2 extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView result = new TextView(this);
  Intent i = getParent().getIntent();
  int a = i.getIntExtra("a", 0);
  int b = i.getIntExtra("b", 0);
  int sum = a + b;
  result.setText(Integer.toString(sum));
        setContentView(result);
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文