Android应用开发学习笔记之使用Bundle交换数据
首先看主布局文件main.xml:
[html]
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入第一个数字:" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入第二个数字:" />
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:text="提交"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />"
</LinearLayout>
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入第一个数字:" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入第二个数字:" />
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:text="提交"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />"
</LinearLayout>
下面来看主Activity文件,其内容如下:
[java]
package com.liuhaoyu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edit1 = (EditText)findViewById(R.id.editText1);
final EditText edit2 = (EditText)findViewById(R.id.editText2);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String num1 = edit1.getText().toString();
String num2 = edit2.getText().toString();
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("number1", num1);
bundle.putCharSequence("number2", num2);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
package com.liuhaoyu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
impor
补充:移动开发 , Android ,