码迷,mamicode.com
首页 > 其他好文 > 详细

1.1Activity跳转与传值

时间:2016-06-02 17:57:15      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

1.跳转:意图的2种形式 class和action(intent-filter要加意图过滤),开启Activity即可

2.传值:意图put/get值

3.日志记录:Log.i    i---info信息记录

 

Manifest.xml:清单文件

技术分享
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manusas.activitydemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".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几个配置-->
        <activity android:name=".NextActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="nextaction" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
View Code

 

MainActivity.java

技术分享
package com.manusas.activitydemo;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1跳转
                // 方法一二 根据意图Class
                //方法一 最常用
               Intent i = new Intent(MainActivity.this, NextActivity.class);
                //方法二
//                Intent i = new Intent();
//                i.setClass(MainActivity.this, NextActivity.class);

                // 方法三四 根据意图Action 在清单文件里面activity里面<intent-filter>一定要定义action名
                //方法三
                //Intent i = new Intent("nextaction");
                //方法四
//                Intent i = new Intent();
//                i.setAction("nextaction");

                //2传值
                i.putExtra("name","liang");
                i.putExtra("age",40);
                Bundle bundle=new Bundle();
                bundle.putString("nickname","manusas");
                i.putExtra("bundle",bundle);
                startActivity(i);
            }
        });
    }
}
View Code

 

NextActivity.java

技术分享
package com.manusas.activitydemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class NextActivity extends Activity {
    public static final String TAG = "tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
        //1取值
        Intent i = getIntent();
        String name = i.getStringExtra("name");
        int age = i.getIntExtra("age", 0);
        Bundle bundle = i.getBundleExtra("bundle");
        String nickname = bundle.getString("nickname");
        Log.i(TAG, name + nickname + age);
    }
}
View Code

 

activity_main.xml:主页

技术分享
<?xml version="1.0" encoding="utf-8"?>
<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="com.manusas.activitydemo.MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
View Code

 

activity_next.xml:跳转页

技术分享
<?xml version="1.0" encoding="utf-8"?>
<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="com.manusas.activitydemo.MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ok"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
View Code

 

1.1Activity跳转与传值

标签:

原文地址:http://www.cnblogs.com/manusas/p/5428132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!