(008)Intent在Activity中的穿梭

本节涉及的知识并不是Intent的基本属性 而是项目中Intent最常用的方法
A、首先第一个重要的用法是Intent(有关于Intent的属性在后面的章节出现)实现页面之间的跳转
在主页面中应该设计成这样:

/*页面的跳转
 * 创建一个xml文件button按钮
 * 设置属性
 * 创建一个点击事件监听器
 * 创建一个intent类
 * Intent intent=new Intent(Activity.this,目标Activity.class)
 * 启动界面  startActivity(intent)
 * 创建一个界面extends Activity
 * 重写onCreat方法
 * 设置页面(xml文件)
 * 注册界面
 * */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //点击事件监听器
    public void intoInfo(View v)
    {
        //点击按钮, 进入内容页面

        //1, 创建Intent意图对象
        /**
         * 第一个参数:   上下文对象    当前Activity
         * 第二个参数:   目标Activity  .class
         */
        Intent intent = new Intent(MainActivity.this, InfoActivity.class);

        //2, 启动Activity
        startActivity(intent);
    }

}

因为Intent的主要作用是实现页面之间的跳转
至少设计到两个页面的内容 所以会在这里会出现跳转的第二个界面

//1, 定义一个类, 继承Activity
public class InfoActivity extends Activity {

    //2, 重写onCreate() 生命周期方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        //设置要显示的页面
        setContentView(R.layout.activity_info);
    }

    //3, 在清单文件中注册
}

清单文件注册是这样的:

 <!-- 注册Activity -->
        <activity android:name="com.qf.day05_activity_demo_01.InfoActivity"/>

B第二个有关于Activity’的知识其实是Intent传递 ……
第一个知识点是说Intent可以实现Activity的页面跳转 第二个知识点是Intent可是实现Activity之间数据的传递
首先需要在MianActivity中写一个传值的方法

public void intoInfo(View v) {

        //1, 创建意图对象
        Intent intent = new Intent(MainActivity.this, InfoActivity.class);

        //Intent传值的第一种方式
        intent.putExtra("msg", "向InfoActivity中传入参数");
        intent.putExtra("name", "张三");


        //Intent传值的第二种方式
        Bundle bundle = new Bundle();
        bundle.putInt("age", 18);
        bundle.putString("sex", "男");

        intent.putExtras(bundle);

        //2, 启动Activity
        startActivity(intent);
    }

其次在InfoActivity中接受传过来的数据即可

        //获取传入的参数
        //1, 得到Intent对象
        Intent intent  = getIntent();

        //2, 获取数据
        //Intent 第一种方式传值,  获取传入的参数
        String msg = intent.getStringExtra("msg");
        String name = intent.getStringExtra("name");

        //Intent 第二种方式传值, 获取传入的参数
        Bundle bundle = intent.getExtras();
        int age = bundle.getInt("age");
        String sex = bundle.getString("sex");

        tv.setText(msg+"\n"+name+"\n"+age+"\n"+sex);

C看完Intent的这两个用法 是不是觉得intent很神奇了,可是实现两个页面的之间的跳转,犹如闪现一样,从一个页面闪现到另一个页面。其实这只是Intent的冰山一角。下面给大家拿出Intent的冰山一大角来看看
几个常用的功能
布局文件是这样的:

 <!-- 子标签只能有一个 -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <Button
                android:id="@+id/but1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="显式启动一" />

            <Button
                android:id="@+id/but2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="显式启动二" />

            <Button
                android:id="@+id/but3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="显式启动三" />

            <Button
                android:id="@+id/but4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="隐式启动" />

            <Button
                android:id="@+id/but5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="打开设置页面" />

            <Button
                android:id="@+id/but6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="打开网页" />

            <Button
                android:id="@+id/but7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="读取图片" />

            <Button
                android:id="@+id/but8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="读取文件" />

            <Button
                android:id="@+id/but9"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="读取视频" />

            <Button
                android:id="@+id/but10"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="读取音频" />

             <Button
                android:id="@+id/but11"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="clickButton"
                android:text="发送短信" />
        </LinearLayout>
    </ScrollView>

MianActivity是这样的:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void clickButton(View v) {
         //创建intent对象
        Intent intent = new Intent();

        switch (v.getId()) {//按钮得到id

        case R.id.but1:
             //跳转页面   显示启动  Intent intent=new Intent()1,2,3,4,5,6,7,8,9,10,11都省略不写提取出来 放到switch上面
            intent = new Intent(MainActivity.this, InfoActivity.class);

            break;

        case R.id.but2:
             //intent调用.setClass()方法
            intent.setClass(MainActivity.this, InfoActivity.class);

            break;

        case R.id.but3:
              //定义ComponentName 参数为本界面和要跳转的界面,用Intent对象调用component()方法参数为component
            ComponentName component = new ComponentName(MainActivity.this,
                    InfoActivity.class);
            intent.setComponent(component);

            break;

        case R.id.but4:

            intent.setAction("com.demo02.secondActivity");

            break;

        case R.id.but5:

            // 打开设置界面
            intent.setAction("android.settings.SETTINGS");

            break;

        case R.id.but6:
                        //系统自带的
            intent.setAction(Intent.ACTION_VIEW);

            // 设置必要的属性

            // URI 统一资源标识符
            // 网址: http://
            // 文件夹: File://
            // 打电话: tel:110112119
            // 发短信: smsto:110
               //data指定action后将必要的数据放入data中 统一资源标示符解析网址
            intent.setData(Uri.parse("http://baidu.com"));

            break;

        case R.id.but7:
            // 读取图片

            intent.setAction(Intent.ACTION_VIEW);
                //                          data属性是路径                      文件类型
            intent.setDataAndType(Uri.parse("file://storage/sdcard/lf.jpg"),
                    "image/*");

            break;

        case R.id.but8:

            intent.setAction(Intent.ACTION_VIEW);
            //                                  data的属性是路径                                                         文件类型
            intent.setDataAndType(Uri.parse("file://storage/sdcard/123.txt"), "text/*");

            break;

        case R.id.but9:

            intent.setAction(Intent.ACTION_VIEW);
            //                                     data的属性是路径                                                   文件类型
            intent.setDataAndType(Uri.parse("file://storage/sdcard/movei.mp4"), "video/*");

            break;

        case R.id.but10:

            intent.setAction(Intent.ACTION_VIEW);
            //                               Data属性是一个路径                                                               指定文件类型
            intent.setDataAndType(Uri.parse("file://storage/sdcard/wlxp.mp3"), "audio/*");

            break;

        case R.id.but11:

            intent.setAction(Intent.ACTION_SENDTO);
            //                         打电话
            intent.setData(Uri.parse("smsto:10086"));
                        //附加内容,传真
            intent.putExtra("sms_body", "今天大家过节了");


            break;
        }

        startActivity(intent);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值