package mars.activity02; 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; public class Activity02 extends Activity { /** Called when the activity is first created. */ private Button myButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //1.得到按钮 myButton = (Button) findViewById(R.id.myButton); myButton.setText("按钮"); //2.将监听器绑定到按钮上 myButton.setOnClickListener(new MyButtonListener()); } //定义一个类来继承 OnClickListener接口(必须是OnClickListener--android.view.View) class MyButtonListener implements android.view.View.OnClickListener{ //复写方法 public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); //从哪跳转到哪,第一个参数是当前Activity,第二个参数是跳转到类的名字 intent.setClass(Activity02.this, OtherActivity.class); Activity02 继承于 Activity, 所以可以使用 startActivity这个方法 Activity02.this.startActivity(intent); } } } package mars.activity02; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class OtherActivity extends Activity { private TextView myTextView = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //1.关联布局文件 setContentView(R.layout.other); //2.通过Id得到控件 myTextView = (TextView)findViewById(R.id.myTextView); //3.通过string.xml设置的键值对,来引用R.string的id myTextView.setText(R.string.other); } } <!-- 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" > <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </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="hello">Hello World, Activity02!</string> <string name="app_name">Activity02</string> <string name="other">otherActivity</string> </resources> <!-- AndroidManifest --> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mars.activity02" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Activity02" 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 --> <activity android:name=".OtherActivity" android:label="@string/other"> </activity> </application> <uses-sdk android:minSdkVersion="4" /> </manifest>