1 .项目目录结构:
2.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test2_3"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="2" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ActivityMain"
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=".OtherActivity"
android:label="@string/other">
</activity>
</application>
</manifest>
3 strings.xml 变量定义
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ActivityMain!</string>
<string name="app_name">我的第一个应用</string>
<string name="abc">字母abc</string>
<string name="other">otherWord</string>
</resources>
4 ActivityMain.java
package com.test2_3;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ActivityMain extends Activity {
/** Called when the activity is first created. */
private Button myButn1=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButn1=(Button) findViewById(R.id.buttonNew);
myButtonListener my1=new myButtonListener();
myButn1.setOnClickListener(my1);
}
class myButtonListener implements OnClickListener , android.view.View.OnClickListener
{
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
public void onClick(View v) {
// TODO Auto-generated method stub
//生成一个Intent对象
Intent intent=new Intent();
intent.putExtra("testIntent", "123");
intent.setClass(ActivityMain.this, OtherActivity.class);
ActivityMain.this.startActivity(intent);
}
}
}
5 main.xml ActivityMain.java的布局文件
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/mytextID"
>
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/abc"
/>
<Button android:text="Button"
android:id="@+id/buttonNew"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<CheckBox android:text="CheckBox11"
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
</LinearLayout>
6 OtherActivity.java
package com.test2_3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity{
TextView myTextView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
Intent intent=getIntent();
String value=intent.getStringExtra("testIntent");
myTextView=(TextView)findViewById(R.id.mytext_other);
myTextView.setText(value);
}
}
7 other.xml OtherActivity.java的布局文件
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/mytext_other"
>
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/abc"
/>
</LinearLayout>
运行效果