Activity01.java
package huang.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;public class Activity01 extends Activity {
/** Called when the activity is first created. */
private EditText myEditText1;
private EditText myEditText2;
private TextView myTextView;
private Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(R.string.myTextView);
myButton = (Button)findViewById(R.id.myButton);
myButton.setText(R.string.myButton);
myButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
String edit1Str = myEditText1.getText().toString();
String edit2Str = myEditText2.getText().toString();
Intent intent = new Intent();
intent.putExtra("one", edit1Str);
intent.putExtra("two", edit2Str);
intent.setClass(Activity01.this, Activity02.class);
Activity01.this.startActivity(intent);
}
}
}main.xml
<?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" > <EditText
android:id="@+id/myEditText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/myEditText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/></LinearLayout>Activity02.java
package huang.activity;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Activity02 extends Activity {
private TextView myTextView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
myTextView2 = (TextView)findViewById(R.id.myTextView02);
Intent intent = getIntent();
String edit1Str = intent.getStringExtra("one");
String edit2Str = intent.getStringExtra("two");
int edit1Int = Integer.parseInt(edit1Str);
int edit2Int = Integer.parseInt(edit2Str);
int result = edit1Int*edit2Int;
myTextView2.setText(result + "");
}
}activity02.xml
<?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:id="@+id/myTextView02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
注:Activity02已经注册,是不是Intent那里的问题啊
package huang.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;public class Activity01 extends Activity {
/** Called when the activity is first created. */
private EditText myEditText1;
private EditText myEditText2;
private TextView myTextView;
private Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(R.string.myTextView);
myButton = (Button)findViewById(R.id.myButton);
myButton.setText(R.string.myButton);
myButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
String edit1Str = myEditText1.getText().toString();
String edit2Str = myEditText2.getText().toString();
Intent intent = new Intent();
intent.putExtra("one", edit1Str);
intent.putExtra("two", edit2Str);
intent.setClass(Activity01.this, Activity02.class);
Activity01.this.startActivity(intent);
}
}
}main.xml
<?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" > <EditText
android:id="@+id/myEditText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/myEditText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/></LinearLayout>Activity02.java
package huang.activity;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Activity02 extends Activity {
private TextView myTextView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
myTextView2 = (TextView)findViewById(R.id.myTextView02);
Intent intent = getIntent();
String edit1Str = intent.getStringExtra("one");
String edit2Str = intent.getStringExtra("two");
int edit1Int = Integer.parseInt(edit1Str);
int edit2Int = Integer.parseInt(edit2Str);
int result = edit1Int*edit2Int;
myTextView2.setText(result + "");
}
}activity02.xml
<?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:id="@+id/myTextView02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
注:Activity02已经注册,是不是Intent那里的问题啊
String edit1Str = myEditText1.getText().toString();
String edit2Str = myEditText2.getText().toString();myEditText1没有初始化,在onCreate里面初始化之后再使用