一天一点Android知识之intent(1)

昨天学习了分析整个project的结构及各个文件的作用,创建运行了最基本的Hello World活动程序,在app的使用中我们会有多个活动之间相互切换,例如微信中chats,Contacts,Discover和Me几个按钮一样,这里写图片描述每当我们点击时便会由一个界面切换到另一个界面,逻辑代码上对应的便是Activity之间的切换,intent完成活动之间的衔接。

接下来介绍程序的具体实现这里写图片描述
FirstActivity.java

package com.example.activitytest;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

***FirstActivity***
public class FirstActivity extends AppCompatActivity {
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        /*getMenuInflater()得到MenuInflate()对象
        再调用它的inflate方法就可以给当前活动创建菜单
        inflate有两个参数,第一个是指定通过哪一个资源文件来创建菜单(R.menu.main)
        第二个是指定我们的菜单项将添加到哪一个Menu对象当中,这里直接使用onCreateOptionsMenu
        传入的Menu参数
        return true表示允许创建的菜单显示出来
        */
        return true;
        //return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){//判断点击的是哪一个菜单项
            case R.id.add_item:
                Toast.makeText(this,"You clicked Add",Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove_item:
                Toast.makeText(FirstActivity.this, "You clicked Remove", Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
       // return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 1:
                if(requestCode == RESULT_OK){
                    String returnedData = data.getStringExtra("data_return");
                    Log.d("FirstActivity",returnedData);
                }
                break;
            default:
        }
        //super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);//传入布局文件的id(R.layout.first_layout)
        //项目中任何资源在都会在R文件中生成相应的资源id
        Button button1 =(Button) findViewById(R.id.button_1);
        //findViewById(R.id.button_1)获取布局文件中定义的元素,传入R.id.button_1获取按钮的实例
        //这个值是刚才first_layout.xml中通过android:id属性指定的
        //返回值是一个View对象,需要向下转型为Button对象
        button1.setOnClickListener(new View.OnClickListener() {
            //为button1注册一个监听器,点击按钮时onClick方法
            // @Override
            public void onClick(View v) {
             /*   //Toast的功能实现是在onClick中
                Toast.makeText(FirstActivity.this,"You clicked Button 1",
                        Toast.LENGTH_SHORT).show();
                //通过静态方法makeText()创建出一个Toast对象
                //调用show()将Toast显示出来就可以
                /*三个参数   1) Context,即Toast要求的上下文,活动本身就是一个context对象
                    2)Toast显示的文本内容
                    3)Toast显示的时长 Toast.LENGTH_SHORT和Toast.LENGTH_LONG
                */

              /*  finish();              */

            /*    Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                startActivity(intent);
                /*显示使用Intent
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class); 其中一个构造函数
                intent的意图:第一个参数为启动活动的上下文,第二个参数class指定想要启动的活动
                即在FirstActivity这个活动的基础上打开SecondActivity这个活动
                通过startActivity()这个方法来执行这个Intent
                */

              /*  Intent intent = new Intent("com.example.activitytest.ACTION_START");
                intent.addCategory("com.example.activitytest.MY_CATEGORY");
                startActivity(intent);
                /*隐式使用intent
                另外一个构造函数,直接将action的字符串传了进去,表明我们想启动能够响应com.example.activitytest.ACTION_START
                 这个action的活动
                 每个Intent只能指定一个action,但却能指定多个category
                */

              /*  Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
                /* action:Intent.ACTION_VIEW是一个内置动作
                Intent.ACTION_VIEW将一个网址字符串解析为一个Uri对象
                调用setData将这个Uri对象传递进去
                * */
              /*  Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);
                */

             /*   String data = "Hello SecondActivity";
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                intent.putExtra("extra_data",data);
                startActivity(intent);    */

                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }
}

SecondActivity.java

package com.example.activitytest;

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

public class SecondActivity extends AppCompatActivity {
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("data_return","Hello FirstActivity");
        setResult(RESULT_OK,intent);
        finish();
        //super.onBackPressed();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
     /*   Intent intent = getIntent();//获取启动SecondActivity的Intent
        String data = intent.getStringExtra("extra_data");
        Log.d("SecondActivity",data);   */

        Button button2 = (Button) findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v){
            Intent intent = new Intent();
            intent.putExtra("data_return","Hello FirstActivity");
            setResult(RESULT_OK,intent);
            finish();
        }
        });
    }
}

ThirdActivity.java

package com.example.activitytest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class ThirdActivity extends AppCompatActivity {

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

first_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1"/>

    <!-- 添加了一个button元素,并为其添加几个属性
    1)android:id="@+id/button_1"  定义唯一的标识符(@+id/id_name)
      引用(@id/id_name)
    2)android:layout_width="match_parent"  当前元素的宽度(和父元素一样宽)
    3)android:layout_height="wrap_content"  当前元素的高度(刚好包含里面的内容)
    4)android:text="Button 1"  指定元素中显示的文字内容
    -->
</LinearLayout>
<!-- LinearLayout元素 -->

second_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2"/>
</LinearLayout>

third_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 3"/>
</LinearLayout>

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".FirstActivity"
            android:label="This is FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--
         activity完成了活动FirstActivity的注册(android studio自动完成)
         在Activity标签中使用android:name来指定具体注册哪一个活动
         能采用.FirstActivity由于package的限定而采用的缩写
         com.example.activitytest.FirstActivity
         android studio自动的完成了活动的注册,但是主活动还是得自己指定
        -->

        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.activitytest.ACTION_START" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.activitytest.MY_CATEGORY" />
            </intent-filter>
            <!--
             <action>标签指明当前活动可以响应的action,
             <category>标签包含一些附加消息,更加精确的指明当前活动能够响应的Intent还有可能带有的category
             只有<action>和<category>标签中的内容能同时匹配上Intent中指定的action和category时,这个活动
             才能够响应该Intent
            -->
        </activity>
        <activity android:name=".ThirdActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

程序运行的结果便是三个活动之间来回切换;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值