(六)——从Activity返回数据

最后更新于:2022-04-01 20:16:32

1、在实际应用中,我们不仅要向Activity中传数据,也要从Activity中返回数据。虽然传递数据和返回数据类似,也可以采用前面四篇中提到的4种方法,但是一般建议采用Intent对象的方式来返回数据,使用这种方式返回数据,需要使用startActivityForResult方法来显示Activity; 2、新建Android项目“android_intent_forresult”,打开布局文件“activity_main.xml”,添加“LinearLayout”、“TextView”、“EditView”等标签,代码如下: ~~~ ~~~ 效果: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-23_57bc06b2911c2.jpg) 3、新建布局文件“other.xml”,添加“TextView”、“EditView”、“Button”标签,代码如下: ~~~ ~~~ 4、新建“OtherActivity.java”文件,并使其继承“Activity”,添加“onCreate”方法,代码如下: ~~~ package com.android.myintent; import android.app.Activity; import android.os.Bundle; public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); } } ~~~ 5、在“AndroidManifest.xml”清单文件中加入“Activity”,加入代码: ~~~ ~~~ 6、在“Main.java”中添加Button成员和“setOnClickListener”,实现两个Button的跳转,点击第一个Activity后,出现第二个Activity;在此方法内部创建意图,用“startActivityForResult”启动意图,并在Main类里重写“onActivityResult”;添加“EditText”成员,实现数据的输入并传入Intent中。代码如下: ~~~ package com.android.myintent; import android.R.integer; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Main extends Activity { private Button button; private final static int REQUESTCODE = 1;// 表示返回的结果码 private EditText one, two, result; // 数据输入 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); one = (EditText) this.findViewById(R.id.one); two = (EditText) this.findViewById(R.id.two); result = (EditText) this.findViewById(R.id.result); button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // 点击后获得用户录入的值 int a = Integer.parseInt(one.getText().toString()); int b = Integer.parseInt(two.getText().toString()); // 创建意图 Intent intent = new Intent(Main.this, OtherActivity.class); // 将值传入意图 intent.putExtra("a", a); intent.putExtra("b", b); startActivityForResult(intent, REQUESTCODE);// 表示可以返回结果 } }); } // 再重写一个onActivityResult方法,作用是将当前Activity中的数据传递到另一个Activity的意图中后,实现跳转,再回传回来。 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } ~~~ 7、在“OtherActivity.java”文件中添加Button和TextView成员,获取意图中的数据,代码如下: ~~~ package com.android.myintent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; public class OtherActivity extends Activity { private Button button; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); // 实例化button和textview button = (Button) this.findViewById(R.id.button2); textView = (TextView) this.findViewById(R.id.msg); Intent intent = getIntent(); // 获取Intent // 取出Intent中的值 int a = intent.getIntExtra("a", 0); int b = intent.getIntExtra("b", 0); textView.setText(a + " + " + b + " = " + " ? "); } } ~~~ 运行一下,看下效果: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-23_57bc06b2c4c38.jpg) 点击“计算结果”,跳转到第二个Activity: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-23_57bc06b2ef364.jpg) 8、回到“Main.java”文件中,从OtherActivity中获取数据并显示,代码如下: ~~~ package com.android.myintent; import android.R.integer; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Main extends Activity { private Button button; private final static int REQUESTCODE = 1;// 表示返回的结果码 private EditText one, two, result; // 数据输入 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); one = (EditText) this.findViewById(R.id.one); two = (EditText) this.findViewById(R.id.two); result = (EditText) this.findViewById(R.id.result); button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // 点击后获得用户录入的值 int a = Integer.parseInt(one.getText().toString()); int b = Integer.parseInt(two.getText().toString()); // 创建意图 Intent intent = new Intent(Main.this, OtherActivity.class); // 将值传入意图 intent.putExtra("a", a); intent.putExtra("b", b); startActivityForResult(intent, REQUESTCODE);// 表示可以返回结果 } }); } // 再重写一个onActivityResult方法,作用是将当前Activity中的数据传递到另一个Activity的意图中后,实现跳转,再回传回来。 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == 2) {// 如果第二个Activity(OtherActivity)正常结束(“2”为返回码resultCode)。 if (requestCode == REQUESTCODE) {// 如果返回状态为1,即成功返回,就在意图的返回值中取出数据。 int three = data.getIntExtra("three", 0);// 从第二个Activity中返回意图中的数据。 result.setText(String.valueOf(three)); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } ~~~ 9、在“OtherActivity.java”文件中,添加点击Button事件,使数据回传~,代码如下: ~~~ package com.android.myintent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class OtherActivity extends Activity { private Button button; private TextView textView; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); // 实例化button和textview button = (Button) this.findViewById(R.id.button2); textView = (TextView) this.findViewById(R.id.msg); editText = (EditText) this.findViewById(R.id.three); Intent intent = getIntent(); // 获取Intent // 取出Intent中的值 int a = intent.getIntExtra("a", 0); int b = intent.getIntExtra("b", 0); textView.setText(a + " + " + b + " = " + " ? "); // 添加点击事件并回传数据 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent();// 重新声明一个意图。 int three = Integer.parseInt(editText.getText().toString());// 获取输入的值。 intent.putExtra("three", three); // 将three回传到意图中。 // 通过Intent对象返回结果,调用setResult方法。 setResult(2, intent);// resultCode为大于1的数,随意选取,为2即可。 finish();// 结束当前Activity的生命周期。 } }); } } ~~~ 10、运行,结果: (1)输入2和3: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-23_57bc06b32712a.jpg) (2)单击“计算结果”,跳转: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-23_57bc06b34a6cf.jpg) (3)输入5,单击“返回结果”,数据回传: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-23_57bc06b373635.jpg) 实现要点: (1)在“Main.java”中,创建Intent并启动Activity,调用“startActivityForResult”,并定义当前请求码; (2)重写“onActivityResult”方法,并设置条件,若满足返回码值,则将第二个Activity中的数据传回来,赋给当前Activity的“result”编辑框; (3)在“OtherActivity.java”中,再创建一个意图,将数据填写到意图中,通过意图将结果回传(通过“setResult”方法); (4)结束当前Activity生命周期;
';