在android eclipse上将数据从一个屏幕传输到另一个屏幕

时间:2012-05-30 05:52:28

标签: android eclipse screen switching

嗨,我是java和android的新手。

假设screenone中的函数demo()将在同一屏幕(screenone)上的Textview上显示一些值。

但是我需要将结果值显示到下一个屏幕,即(屏幕2)

public void demo(){
{
 .....
 .....
}

所以我把这些行包括在

screenoneActivity.java

Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
demo();

ScreentwoActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main1);

    TextView txtName = (TextView) findViewById(R.id.textView1);

    Intent i = getIntent();

    txtName.setText(name);

到目前为止我做了这些事情。我不知道如何将数据从demo()功能传输到下一个屏幕。

任何人都可以给我提供线索或想法来实现这一目标。

非常感谢!..

4 个答案:

答案 0 :(得分:2)

你需要在putExtra方法参数中发送一些值才能从中获取一些东西。

在您的第一项活动(A)中:

Intent i = new Intent(A.this, B.class);
i.putExtra("someName", variableThatYouNeedToPass);
startActivity(i);

在你的第二项活动(B)中:

Bundle extras = getIntent().getExtras();
int fetchedVariable = extras.getInt("someName");

答案 1 :(得分:1)

在demo()函数中编写以下代码:

Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
       nextScreen.putExtra("","");
       startActivity(nextScreen); 

nextScreen.putExtra("","");中提供一些关键字和值,如:

nextScreen.putExtra("name","ABC");

现在在SecondActivity中,写一下:

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

    setContentView(R.layout.main1);

    TextView txtName = (TextView) findViewById(R.id.textView1);

    Intent i = getIntent();
    Bundle bundle = i.getExtras();

    txtName.setText(bundle.getString("name"));

答案 2 :(得分:1)

在ScreenoneActivity中

Intent act2=new Intent(this,Activity2.class);
    act2.putExtra("A",a);
    startActivity(act2);

在ScreentwoActivity类

Intent i = getIntent();
Bundle extras = getIntent().getExtras(); 
int a = extras.getInt("A");
txtName.setText(a);

答案 3 :(得分:0)

在onCreate中

Bundle extras = getIntent().getExtras(); 
String value;

if (extras != null) 
{
    value= extras.getString("key");
}

https://stackoverflow.com/questions/10752501/how-can-we-go-to-next-page-in-android/10752516#10752516

谷歌这是非常基本的.....

android使用intent ....

Vogella Ariticle

活动1中的

-

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");

startActivity(i);

在活动2中 - 在onCreate finction

Bundle extras = getIntent().getExtras();

if (extras == null) {
        return;
        }
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
    // Do something with the data
}