在软件中所有的数据不可能在同一个页面或者是同一个类中进行处理,这就涉及到数据直接的传递,在web中数据要传递给处理页面进行数据处理是通过form表单进行传递的,在Android中数据直接的传递是通过Bundle把数据压到栈中进行数据传递,然后通过.getIntent().getExtras()获取到里面的数据,下面是一个简单是数据之间的传递。
首先在第一个Android Activity中放两个EditView,然后用户通过输入两个数据并传到另外一个activity中,然后计算两个数的和显示结果。
首先在main.xml中定义两个EditView和一个Button:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"计算数据"
/>
<
EditText
android:id
=
"@+id/data1"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/>
<
EditText
android:id
=
"@+id/data2"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/>
<
Button
android:id
=
"@+id/button"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"计算"
/>
</
LinearLayout
>
|
创建第一个activity类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package
com.data;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.EditText;
public
class
ActivityData
extends
Activity {
/** Called when the activity is first created. */
private
Button button;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(
new
OnClickListener() {
public
void
onClick(View v) {
EditText data1 = (EditText) findViewById(R.id.data1);
EditText data2 = (EditText) findViewById(R.id.data2);
Intent intent =
new
Intent();
Bundle bundle =
new
Bundle();
bundle.putString(
"value1"
, data1.getText().toString());
bundle.putString(
"value2"
, data2.getText().toString());
intent.putExtras(bundle);
intent.setClass(ActivityData.
this
, ResultActivity.
class
);
startActivity(intent);
ActivityData.
this
.finish();
}
});
}
}
|
把用户输入的数据放在Bundle中,通过Intent传递到另外一个activity。
另外在layout文件夹中创建一个mylayout.xml配置文件,里面添加一个EditView显示标签用了显示计算结果。
1
2
3
4
5
6
7
8
9
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:id
=
"@+id/result"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/>
</
LinearLayout
>
|
创建另外接收数据的activity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package
com.data;
import
android.app.Activity;
import
android.os.Bundle;
import
android.widget.TextView;
public
class
ResultActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
Bundle bundle =
this
.getIntent().getExtras();
Double data1 = Double.parseDouble(bundle.getString(
"value1"
));
Double data2 = Double.parseDouble(bundle.getString(
"value2"
));
Double result = data1 + data2;
TextView textView = (TextView) findViewById(R.id.result);
textView.setText(
"result ======== "
+ result);
}
}
|
获取第一个activity传过来的数据进行计算并显示结果。
在AndroidManifest.xml中注册activity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
package
=
"com.data"
android:versionCode
=
"1"
android:versionName
=
"1.0"
>
<
uses-sdk
android:minSdkVersion
=
"4"
/>
<
application
android:icon
=
"@drawable/icon"
android:label
=
"@string/app_name"
>
<
activity
android:name
=
".ActivityData"
android:label
=
"@string/app_name"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
<
activity
android:name
=
".ResultActivity"
></
activity
>
</
application
>
</
manifest
>
|
执行结果图: