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"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="Welcome to Cinrry's blog"    /><Button    android:id="@+id/bt1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="点击进入Layout2"/></LinearLayout>


mylayout.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"    android:background="#ffffffff"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="Here's tne intent Activity"    /><Button    android:id="@+id/bt2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="点击进入Layout1"/></LinearLayout>


然后是控制程序IntentDemo.java 及IntentDemo1.java 代码:



IntentDemo.java:
package com.android.test;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button; public class IntentDemo extends Activity {     private Button bt1;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                bt1 = (Button)findViewById(R.id.bt1);                bt1.setOnClickListener(new Button.OnClickListener(){            public void onClick(View v){                //new 一个Intent对象,并指定要启动的Class                Intent intent = new Intent();                              intent.setClass(IntentDemo.this, IntentDemo1.class);                          //调用一个新的Activity                startActivity(intent);                //关闭原本的Activity                IntentDemo.this.finish();             }        });    }}


在IntentDemo.java 同一目录内新建一个IntentDemo1.java 类



IntentDemo1.java:
package com.android.test;import android.app.Activity;import android.content.Intent; import android.os.Bundle;import android.view.View; import android.widget.Button; public class IntentDemo1 extends Activity {    private Button bt2;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 载入mylayout.xml        setContentView(R.layout.mylayout);         bt2 = (Button) findViewById(R.id.bt2);        bt2.setOnClickListener(new Button.OnClickListener() {            public void onClick(View v) {                // new 一个Intent对象,并指定要启动的Class                Intent intent = new Intent();                intent.setClass(IntentDemo1.this, IntentDemo.class);                // 调用一个新的Activity                startActivity(intent);                // 关闭原本的Activity                IntentDemo1.this.finish();             }        });    }}


最后是本例子的重点,添加另外一个Activity 所以必须在AndroidManifest.xml 中定义一个新的activty ,并给予名称name ,或则程序无法编译运行.新手很容易遇到这个问题.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.android.test"      android:versionCode="1"      android:versionName="1.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".IntentDemo"                  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="IntentDemo1"></activity>     </application>    <uses-sdk android:minSdkVersion="3" /></manifest>

更多相关文章

  1. Android中的Message类以及Java对象池的实现
  2. Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支
  3. android仿京东商城例子
  4. android 中通过 aidl 传递map对象
  5. Android IDL 小例子
  6. Android 各种布局技术-五大布局对象

随机推荐

  1. Android(安卓)Parcelable理解与使用(对象
  2. create custom launcher icon 细节介绍
  3. Android(安卓)SQLite总结(一)
  4. android之TimePicker
  5. android task与back stack 开发文档翻译
  6. ActionBar简单使用介绍和Tab切换的应用
  7. 解决Android(安卓)5.0以上版本Button自带
  8. 谈谈android大众常用的五种布局?
  9. android —— 自定义控件 利用 ViewPage
  10. eclipse快捷鍵