MyActivity.java
package zzn.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyActivity extends Activity
{
/** Called when the activity is first created. */
private Button myButton = null;
private EditText myEdit01;
private EditText myEdit02;
private TextView myText01;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myEdit01 = (EditText)findViewById(R.id.edit01);
myEdit02 =(EditText)findViewById(R.id.edit02);
myText01 = (TextView)findViewById(R.id.text01);
myButton = (Button)findViewById(R.id.myButton);
myText01.setText(R.string.textView01);
myButton.setText(R.string.myButton);
myButton.setOnClickListener(new MyButtonListener());
}
// 当点击键盘上的menu按钮时调用此方法。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1,1,1,R.string.exit);
menu.add(1,2,2,R.string.about);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == 1){
finish();
}
return super.onOptionsItemSelected(item);
}
class MyButtonListener implements View.OnClickListener {
@Override
public void onClick(View view) {
String edit01Str = myEdit01.getText().toString();
String edit02Str = myEdit02.getText().toString();
Intent intent = new Intent();
intent.putExtra("edit01",edit01Str);
intent.putExtra("edit02",edit02Str);
intent.setClass(MyActivity.this,MyActivity01.class);
startActivity(intent);
//生成一个Intent对象
/* Intent intent = new Intent();
intent.putExtra("testExtra","123456");
intent.setClass(MyActivity.this,MyActivity01.class);
MyActivity.this.startActivity(intent);*/
/*Uri uri = Uri.parse("smsto://0800000123");
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body","this is sms test");
startActivity(intent);*/
}
}
}
MyActivity02.java
package zzn.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MyActivity01 extends Activity {
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
Intent intent = getIntent();
String edit01Str = intent.getStringExtra("edit01");
String edit02Str = intent.getStringExtra("edit02");
int intEdit01 = Integer.parseInt(edit01Str);
int intEdit02 = Integer.parseInt(edit02Str);
int result = intEdit01*intEdit02;
myTextView = (TextView)this.findViewById(R.id.myTextView);
myTextView.setText(result+"");
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/edit01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/text01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<EditText
android:id="@+id/edit02"
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"
android:text="点击我跳转" />
</LinearLayout>
other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">myAndroid01</string>
<string name="myTextView">otherActivity</string>
<string name="textView01">乘以</string>
<string name="myButton">计算结果</string>
<string name="exit">退出</string>
<string name="about">关于</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zzn.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<application android:label="@string/app_name">
<activity android:name="MyActivity"
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="MyActivity01"
android:label="@string/myTextView"/>
</application>
</manifest>