[英]Transfer data from one screen to another on android eclipse
嗨,我是Java和android的新手。
假设来自screenone的功能demo()
将在同一屏幕(screenone)上的Textview
上显示一些值。
但是我需要在下一个屏幕上显示该结果值。(屏幕二)
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()
函数传输到下一个屏幕。
任何人都可以给我线索或想法来实现这一目标。
非常感谢!..
您需要将一些值发送到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");
在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"));
在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);
在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使用意图...
在活动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函数中
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.