首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Intent对象的简略使用

2012-08-21 
Intent对象的简单使用LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android??? and

Intent对象的简单使用
<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="欢迎来到魏祝林的博客"
??? />
<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="Welcome to Mr Wei's blog"
??? />
<Button
??? android:id="@+id/bt2"
??? android:layout_width="wrap_content"
??? android:layout_height="wrap_content"
??? android:text="点击进入Layout1"
/>

</LinearLayout>

然后是控制程序IntentDemo.javaIntentDemo1.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>

本例子所涉及的的全部代码已经全部贴出,最后执行之,将达到上述效果!本节到此结束~

?

热点排行