我是android的初学者,我正在努力学习如何使用意图。 在我的代码中,我试图将2个整数发送到不同的活动并执行一些计算并返回主活动中的答案。
这是我的主要活动,点击一个按钮,它应该向我的计算类发送10,50,然后从那里我将点击操作按钮,如添加,乘法,除法,并在主要活动中发回答案。 / p>
到目前为止,我能够将这些数字收到我的Calculate课程并在那里进行操作,但我不知道如何在主要活动中找回答案。
主要活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(getIntent() != null){
String msg = getIntent().getStringExtra("MSG");
int result = getIntent().getIntExtra("result", 0);
Toast.makeText(this, "" + msg + result, Toast.LENGTH_LONG).show();
}
}
public void onClick(View v){
Intent i = new Intent(this, Calculate.class);
i.putExtra("MSG", "Receive two numbers : ");
i.putExtra("second", 10);
i.putExtra("first", 50);
startActivity(i);
}
}
计算班级
public class Calculate extends Activity{
private int calc;
private int first;
private int second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i = getIntent();
if(getIntent() != null){
String msg = i.getStringExtra("MSG");
first = i.getIntExtra("first", 0);
second = i.getIntExtra("second", 0);
Toast.makeText(this, "" + msg + first + " and " + second, Toast.LENGTH_LONG).show();
}
}
public void add(View v){
calc = first + second;
Intent i = new Intent(this, MainActivity.class);
i.putExtra("MSG", "Answer");
i.putExtra("result",calc);
}
}
答案 0 :(得分:2)
您可以使用startActivityForResult和onActivityResult方法。在主活动中,您调用startActivityForResult,这意味着您希望从secondActivity(Calculate activity)获取结果,然后覆盖主活动中的onActivityResult以获取结果。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(getIntent() != null){
String msg = getIntent().getStringExtra("MSG");
int result = getIntent().getIntExtra("result", 0);
Toast.makeText(this, "" + msg + result, Toast.LENGTH_LONG).show();
}
}
public void onClick(View v){
Intent i = new Intent(this, Calculate.class);
i.putExtra("MSG", "Receive two numbers : ");
i.putExtra("second", 10);
i.putExtra("first", 50);
startActivityForResult(i, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
}
public class Calculate extends Activity{
private int calc;
private int first;
private int second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i = getIntent();
if(getIntent() != null){
String msg = i.getStringExtra("MSG");
first = i.getIntExtra("first", 0);
second = i.getIntExtra("second", 0);
Toast.makeText(this, "" + msg + first + " and " + second, Toast.LENGTH_LONG).show();
}
}
public void add(View v){
calc = first + second;
Intent returnIntent = Intent(this, MainActivity.class);
returnIntent.putExtra("result",calc );
setResult(RESULT_OK,returnIntent);
finish();
}
}
答案 1 :(得分:1)
public class Calculate extends Activity{
private int calc;
private int first;
private int second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i = getIntent();
if(getIntent() != null){
String msg = i.getStringExtra("MSG");
first = i.getIntExtra("first", 0);
second = i.getIntExtra("second", 0);
Toast.makeText(this, "" + msg + first + " and " + second, Toast.LENGTH_LONG).show();
}
}
public void add(View v){
calc = first + second;
Intent i = new Intent(this, MainActivity.class);
i.putExtra("MSG", "Answer");
i.putExtra("result",calc);
startActivity(i);
}
}
startActivity(i);
函数需要add()
。
答案 2 :(得分:1)
您可以通过
执行此操作onActivityResult()
android意图的方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(data.getExtras().containsKey("widthInfo")){
width.setText(data.getStringExtra("widthInfo"));
}
if(data.getExtras().containsKey("heightInfo")){
height.setText(data.getStringExtra("heightInfo"));
}
}
有关详情,请参阅以下链接: http://developer.android.com/training/basics/intents/result.html
http://www.java2s.com/Code/Android/UI/CheckActivityresultandonActivityResult.htm
http://www.mybringback.com/android-sdk/12204/onactivityresult-android-tutorial/
答案 3 :(得分:1)
看起来你应该在这里使用startActivityForResult()
。
因此,在您的MainActivity中,您的onClick()
会被修改为调用startActivityForResult()
:
public void onClick(View v){
Intent i = new Intent(this, Calculate.class);
i.putExtra("MSG", "Receive two numbers : ");
i.putExtra("second", 10);
i.putExtra("first", 50);
startActivityForResult(i, 100); //modified
}
然后在MainActivity中,您还要添加onActivityResult
以获得结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == RESULT_OK){
String msg = data.getStringExtra("MSG");
int result = data.getIntExtra("result", 0);
Toast.makeText(this, "" + msg + result, Toast.LENGTH_LONG).show();
}
}
然后,在Calculate
活动中,您将计算结果,然后将其发送回Intent中的MainActivity
,这会发送MSG
和result
到MainActivity中的onActivityResult()
:
public void add(View v){
calc = first + second;
Intent i = new Intent(); //modified
i.putExtra("MSG", "Answer");
i.putExtra("result",calc);
setResult(RESULT_OK,i); //added
finish(); //added
}
请注意,在这里使用startActivityForResult()
会更好的原因是它可以防止必须在后台堆栈中添加多个活动。
比如说,你做两次计算(从MainActivity到计算和返回两次)。
如果您刚使用startActivity()
,那么您的后筹码将如下所示:
MainActivity->Calculate->MainActivity->Calculate->MainActivity
并且,它会继续增加你做的更多计算。
使用startActivityForResult()
,您只需将Calculate
的实例添加到后台堆栈,然后弹出它并返回到MainActivity
的同一实例。所以,你的后台总是只是:
MainActivity
...或
MainActivity->Calculate
取决于您当前所处的活动。正如您所看到的,这是对第一个选项的巨大改进,随着您进行更多计算,该选项会不断增长。