如何从activity返回数据

如何从activity返回数据,第1张

1、在实际应用中,我们不仅要向Activity中传数据,也要从Activity中返回数据。虽然传递数据和返回数据类似,也可以采用前面四篇中提到的4种方法,但是一般建议采用Intent对象的方式来返回数据,使用这种方式返回数据,需要使用startActivityForResult方法来显示Activity;

2、新建Android项目“android_intent_forresult”,打开布局文件“activity_main.xml”,添加“LinearLayout”、“TextView”、“EditView”等标签,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<EditText

android:id="@+id/one"

android:layout_width="20dp"

android:layout_height="wrap_content" >

</EditText>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" + " >

</TextView>

<EditText

android:id="@+id/two"

android:layout_width="20dp"

android:layout_height="wrap_content" >

</EditText>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" = " >

</TextView>

<EditText

android:id="@+id/result"

android:layout_width="20dp"

android:layout_height="wrap_content" >

</EditText>

</LinearLayout>

<Button

android:id="@+id/button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="计算结果" >

</Button>

</LinearLayout>

3、新建布局文件“other.xml”,添加“TextView”、“EditView”、“Button”标签,代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

 android:orientation="vertical" >

 <LinearLayout

   android:layout_width="fill_parent"

   android:layout_height="wrap_content"

   android:orientation="vertical" >

   <TextView

     android:id="@+id/msg"

     android:layout_width="match_parent"

     android:layout_height="wrap_content" >

   </TextView>

   <EditText

     android:id="@+id/three"

     android:layout_width="match_parent"

     android:layout_height="wrap_content" >

   </EditText>

 </LinearLayout>

 <Button

   android:id="@+id/button2"

   android:layout_width="match_parent"

   android:layout_height="wrap_content"

   android:text="返回结果" >

 </Button>

</LinearLayout>

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”,加入代码:<activity android:name=".OtherActivity" >

       </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 menuthis 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 + " = " + " ? ")

 }

}

运行一下,看下效果:

点击“计算结果”,跳转到第二个Activity:

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 menuthis 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:

(2)单击“计算结果”,跳转:

(3)输入5,单击“返回结果”,数据回传:

实现要点:

(1)在“Main.java”中,创建Intent并启动Activity,调用“startActivityForResult”,并定义当前请求码;

(2)重写“onActivityResult”方法,并设置条件,若满足返回码值,则将第二个Activity中的数据传回来,赋给当前Activity的“result”编辑框;

(3)在“OtherActivity.java”中,再创建一个意图,将数据填写到意图中,通过意图将结果回传(通过“setResult”方法);

(4)结束当前Activity生命周期;

Intent intent = new Intent()

//用intent.putExtra(String name, String value)来传递参数。

intent.putExtra("one",number1)

intent.putExtra("two", number2)

intent.setClass(activity1.this, activity2.class)

startActivity(intent)

然后在activity2中

Intent intent1 = getIntent()

// 用intent1.getStringExtra()来得到activity1发过来的字符串。

String one = intent1.getStringExtra("one")

String two = intent1.getStringExtra("two")

然后在sql语句获取值存储。

Activity不能直接跳转到Fragment,只能够先跳到指定的activity,在intent中指明你要显示的Fragment, 再在此activity 内,显示这个Fragment。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10087372.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存