在 Android 中:如何从一个屏幕到另一个屏幕获取变量/数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2347152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:29:28  来源:igfitidea点击:

In Android: How do I get variables/data from one screen to another?

android

提问by Allan

In android: I'm trying to take data from one activity/screen to another.

在 android 中:我试图将数据从一个活动/屏幕转移到另一个活动/屏幕。

Let's say I'm adding two numbers. I layout my first screen (xml) with 2 EditText views, a couple labels, and an 'OK' button. Now, I want to add the numbers that I've input into the EditText views. Let's say I input 2 and 2 (2 + 2 = 4).

假设我将两个数字相加。我使用 2 个 EditText 视图、几个标签和一个“确定”按钮来布局我的第一个屏幕 (xml)。现在,我想将我输入的数字添加到 EditText 视图中。假设我输入 2 和 2 (2 + 2 = 4)。

Now, when I hit the 'OK' button, I want a new screen/activity to appear and simply show me the answer (4). Do I use global vars to do this? Any help would be appreciated.

现在,当我点击“确定”按钮时,我希望出现一个新屏幕/活动并简单地向我显示答案 (4)。我是否使用全局变量来做到这一点?任何帮助,将不胜感激。

回答by Ally

First Activity

第一次活动

Intent myIntent = new Intent();
myIntent.putExtra("key", "value");
startActivity(myIntent); 

New Activity

新活动

Intent myIntent = getIntent(); // this is just for example purpose
myIntent.getExtra("key");

Check out the different types you can useon the Android Dev Site

查看您可以在 Android 开发站点上使用不同类型

Note: If you're looking for a way of sharing an object/data globally then you could extend the Application class. Check out How to declare global variables in Android?(answer by Soonil)

注意:如果您正在寻找一种全局共享对象/数据的方法,那么您可以扩展 Application 类。查看如何在 Android 中声明全局变量?(Soonil 的回答)

回答by skyman

I suppose that You're starting "next screen" using Intent (it's the way it should be done).

我想您正在使用 Intent 开始“下一个屏幕”(这是应该完成的方式)。

In Intent you can pass extras (putExtra) and in onCreate in "next activity" you can getIntent().getXExtra()(substitute X with field type)

在 Intent 中,您可以传递额外内容 (putExtra),而在“下一个活动”中的 onCreate 中,您可以getIntent().getXExtra()(用字段类型替换 X)

回答by Hubert

Take a look at the Some Intent examplessection (from Common Tasks and How to Do Them in Android):

查看一些意图示例部分(来自常见任务和如何在 Android 中执行):

basically, you use myIntent.putExtra (...) to send data (can be String, Int, Boolean etc) to the other receiving end (the other activity)...

基本上,您使用 myIntent.putExtra (...) 将数据(可以是 String、Int、Boolean 等)发送到另一个接收端(另一个活动)...

then, result will be passed back into the calling Activity's onActivityResult() method:

然后,结果将被传递回调用 Activity 的 onActivityResult() 方法:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    // See which child activity is calling us back.
    switch (resultCode) {
        case CHOOSE_FIGHTER:
            // This is the standard resultCode that is sent back if the
            // activity crashed or didn't doesn't supply an explicit result.
            if (resultCode == RESULT_CANCELED){
                myMessageboxFunction("Fight cancelled");
            } 
            else {
                myFightFunction(data);
            }
        default:
            break;
    }

H.

H。

回答by Joseph Selvaraj

A sample from my project. We have to use Bundle to get the data.

我的项目中的一个示例。我们必须使用 Bundle 来获取数据。

Use the code in the from/first activity yo set the data. You can set all kind of data types including arrays.

使用 from/first 活动中的代码来设置数据。您可以设置所有类型的数据类型,包括数组。

Intent i = new Intent(this, LanguageSetting.class);
i.putExtra("From", 1);
startActivity(i);

How to retrive the data, write the below code in the new/second activity.

如何检索数据,在新的/第二个活动中编写以下代码。

Intent myIntent = getIntent();
Bundle b = myIntent.getExtras();
intCameFrom = b.getInt("From");