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

Intent相关

时间:2014-11-24 22:05:36      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   color   sp   java   

Intent是什么?

翻译为:意图,目的(名词)

其实根本没必要管它是什么,看看它能做什么就好了。

不过后来我知道了,它就是个机制----通信机制-----android的许多组件间的交流要依赖它。

 

下面说一些例子:

一、连接活动

例子的思路很简单:从一个组件跳转到另外一个组件

     1. 组件=UI组件+类组件 ,即,需要一个.java文件,还需要一个layout文件

     2. 用组件就要注册,在AndroidManifest.xml文件中加入代码就是了

 

main_activity.xml

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是第一个活动" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:text="导航到第二个activity" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener
{

    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            
        btn = (Button) findViewById(R.id.btn1);
        btn.setOnClickListener(this);
        
    }
    @Override
    public void onClick(View v) 
    {
        startActivity(new Intent("com.example.intenttest.SecondActivity"));//指定activity的名字
    }
}

这就是第一个组件(建工程的时候,就在AndroidManifest.xml文件中注册过了,不要瞎操心) onClick()里面的这句就是跳转了。

 

下面写第二组件

secondactivity.xml

<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" >
  <TextView 
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="这是第2个活动"/>
</RelativeLayout>

SecondActivity.java

public class SecondActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);
    }
}

注册(在application标签内加入)

      <activity
            android:name="com.example.intenttest.SecondActivity" 
            android:label="@string/activity_second">
            <intent-filter>
                <action android:name="com.example.intenttest.SecondActivity"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

这里有一个intent-filter,表示意图筛选器,其他活动就是通过这个东西来启动本活动的,相当于这货是向外暴露本activity,或者说,做一个说明限定。

(android中定义的活动,可以被任意应用程序调用,所以意图筛选器的名称要写的很清楚)

(当然如果不幸,俩筛选器的名称一样,那系统会提示您做默认选择的;如果要清楚默认,要到setting—> application—>找到应用程序 app info ---> clear default)

bubuko.com,布布扣

点击按钮,就显示第二个activity了。

bubuko.com,布布扣

小结:intent确实可以连接两个activity,关键代码是:

startActivity(new Intent(activity的名字));

补充:如果要启动的activity是本类的activity,那么可以有另外一种写法:

startActivity(new Intent(this, SecondActivity.class));

2.  从意图返回结果

活动活动腿脚再来更新

Intent相关

标签:android   style   blog   http   io   ar   color   sp   java   

原文地址:http://www.cnblogs.com/bluechip/p/4119671.html

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