`
cinrry
  • 浏览: 7321 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Intent调用

阅读更多
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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics