Android笔记 显式意图demo

显示意图需要知道包名 组件(Activity)名等详细信息才能实现跳转

1界面二的布局

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="界面2" />

    <Button
        android:id="@+id/button1"
        android:onClick="click2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="14dp"
        android:text="跳转到界面1" />

</RelativeLayout>


2new class继承Activity

package com.example.a66_intent_1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class Activity2 extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity2);
	}

	public void click2(View view) {
		Intent intent =new Intent();
		intent.setClass(Activity2.this, MainActivity.class);
		startActivity(intent);
	}
}

3在清单文件配置界面2

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a66_intent_1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.a66_intent_1.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="com.example.a66_intent_1.Activity2"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>
4编写界面1布局

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="界面1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:onClick="click"
        android:text="跳转" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="14dp"
        android:onClick="click3"
        android:text="跳转到系统应用" />

</RelativeLayout>


5编写界面1代码

package com.example.a66_intent_1;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	//四种方法来进行显示意图跳转
	public void click(View view) {
		Intent intent =new Intent(MainActivity.this, Activity2.class);
		startActivity(intent);
	}
	
//	public void click(View view) {
//		Intent intent =new Intent();
//		intent.setClassName(MainActivity.this, "com.example.a66_intent_1.Activity2");
//		startActivity(intent);
//	}

//	public void click() {
//		Intent intent =new Intent();
//		intent.setClass(MainActivity.this, Activity2.class);
//		startActivity(intent);
//	}
	//以上三种方式跳转意思一致
	
	public void click3(View view) {
		Intent intent =new Intent();
		//setClassName方法有俩种 此处与24行的是不同的setClassName方法
		//一般系统应用包名可能会被开发商更改 因此可能执行不成功 接下来会学习隐式意图来跳转到系统组件界面
		intent.setClassName("com.sec.android.gallery3d", "com.sec.android.gallery3d.app.Gallery");
		startActivity(intent);
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值