您的位置:首页 > 移动开发 > Android开发

Intent 7大属性之一:ComponentName

2016-01-05 19:28 513 查看
package com.lxj.day06_intent;

import android.os.Bundle;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity {

    //声明控件

    private Button show_next;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        //1、初始化控件

        show_next = (Button) findViewById(R.id.show_next);

        

        show_next.setOnClickListener(new OnClickListener() {

            

            @Override

            public void onClick(View v) {

                // 给Intent设置ComponentName属性

                ComponentName cn = new ComponentName(MainActivity.this,BActivity.class);

                //第二步:将ComponentName设置到intent中

                Intent intent = new Intent();

                intent.setComponent(cn);

                //第三步:startActivity(intent)

                startActivity(intent);

            }

        });

    }

}

----------------------------------------------------------------------------------------------

package com.lxj.day06_intent;

import android.net.Uri;

import android.os.Bundle;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class BActivity extends Activity {

    private Button show_next;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.bactivity_main);

       

        show_next = (Button) findViewById(R.id.show_next);

       

        show_next.setOnClickListener(new OnClickListener() {

           

            @Override

            public void onClick(View v) {

              

                ComponentName cn = new ComponentName(BActivity.this,CActivity.class);

                Intent intent = new Intent();

                intent.setComponent(cn);

                startActivity(intent);

            }

        });

    }

}

---------------------------------------------------------------------------------------------

package com.lxj.day06_intent;

import android.os.Bundle;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class CActivity extends Activity {

    private Button show_next;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.cactivity_main);

        show_next = (Button) findViewById(R.id.show_next);

       

        show_next.setOnClickListener(new OnClickListener() {

           

            @Override

            public void onClick(View v) {

                ComponentName cn = new ComponentName(CActivity.this , DActivity.class);

                Intent intent = new Intent();

                intent.setComponent(cn);

                startActivity(intent);

            }

        });

    }

}

---------------------------------------------------------------------------------------------

package com.lxj.day06_intent;

import android.os.Bundle;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class DActivity extends Activity {

    private Button show_next;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.dactivity_main);

        show_next = (Button) findViewById(R.id.show_next);

        show_next.setOnClickListener(new OnClickListener() {

           

            @Override

            public void onClick(View v) {

                ComponentName cn = new ComponentName(DActivity.this,CActivity.class);

                Intent intent = new Intent();

                intent.setComponent(cn);

                startActivity(intent);

            }

        });

    }

}

----------------------------------------------------------------------------------------------<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    >

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="这是MainActivity" />

    <Button

        android:id="@+id/show_next"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

         android:text="跳转到下个Activity"

        />

</LinearLayout>

----------------------------------------------------------------------------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    >

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="这是BActivity" />

    <Button

        android:id="@+id/show_next"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

         android:text="跳转到下个Activity"

        />

</LinearLayout>

----------------------------------------------------------------------------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    >

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="这是CActivity" />

    <Button

        android:id="@+id/show_next"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

         android:text="跳转到下个Activity"

        />

</LinearLayout>

----------------------------------------------------------------------------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    >

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="这是DActivity" />

    <Button

        android:id="@+id/show_next"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

         android:text="跳转到下个Activity"

        />

</LinearLayout>

--------------------------------------AndroidMainfest.xml-----------------------------------

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.qf.day06_intent"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="14"

        android:targetSdkVersion="19" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.qf.day06_intent.MainActivity"

            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=".BActivity"></activity>

        <activity android:name=".CActivity"></activity>

        <activity android:name=".DActivity"></activity>

    </application>

</manifest>

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android class 学习 交流