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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值