万书网 > 文学作品 > Android从入门到精通 > 第71页

第71页



CATEGORY_APP_MARKET  如果Activity允许用户浏览和下载新应用,则进行设置

说明:  关于表6.3内容的详细说明,请参考API文档中Intent类的说明。

注意:  在使用表6.3中的种类时,需要将其转换为对应的字符串信息。例如将CATEGORY_  DEFAULT转换为android.intent.category.DEFAULT。

addCategory()方法将种类增加到Intent对象中,removeCategory()方法删除上次增加的种类,getCategories()方法获得当前对象中包含的全部种类。

6.1.5 额外(Extras)

Extras是一组键值时,其中包含了应该传递给处理Intent的组件的额外信息。就像一些动作与特定种类的数据URI匹配,而一些与特定额外匹配。例如,动作为ACTION_TIMEZONE_CHANGED的Intent用time-zone额外来表示新时区;动作为ACTION_HEADSET_PLUG的Intent用state额外来表示耳机是否被插入,以及用name额外来表示耳机的类型。如果开发人员自定义一个SHOW_COLOR动作,则应该包含额外来表示颜色值。

Intent对象中包含了多个putXXX()方法(如putExtra()方法)用来插入不同类型的额外数据,也包含了多个getXXX()方法(如getDoubleExtra()方法)来读取数据。这些方法与Bundle对象有些类似。实际上,额外可以通过putExtras()和getExtras()方法来作为Bundle设置和读取。

6.1.6 标记(Flags)

Flags表示不同来源的标记。多数用于指示Android系统如何启动Activity(如Activity属于哪个Task)以及启动后如何对待(如它是否属于近期的Activity列表)。所有标记都定义在Intent类中。

说明:  所有标记都是整数类型。

6.1.7 范例1:在Activity间使用Intent传递信息

例6.1   在Eclipse中创建Android项目,名称为6.1,在Activity中使用Intent来传递信息。(实例位置:光盘\TM\sl\6\6.1)

(1)在res\layout文件夹中创建布局文件firstactivity_layout.xml。在布局文件中,增加文本框、编辑框、按钮等控件,并修改其默认属性。修改完成后为布局代码如下:








android:layout_width="match_parent"



android:layout_height="match_parent"



android:background="@drawable/background"



android:orientation="vertical"  >






android:layout_width="fill_parent"



android:layout_height="wrap_content"



android:gravity="center"



android:text="@string/title"



android:textColor="@android:color/black"



android:textSize="30px"  />






android:layout_width="wrap_content"



android:layout_height="wrap_content"



android:text="@string/username"



android:textColor="@android:color/black"



android:textSize="20px"  />






android:id="@+id/username"



android:layout_width="match_parent"



android:layout_height="wrap_content"



android:textColor="@android:color/black"  >














android:layout_width="wrap_content"



android:layout_height="wrap_content"



android:text="@string/password"



android:textColor="@android:color/black"



android:textSize="20px"  />






android:id="@+id/password"



android:layout_width="match_parent"



android:layout_height="wrap_content"



android:inputType="textPassword"



android:textColor="@android:color/black"  />






android:id="@+id/ok"



android:layout_width="wrap_content"



android:layout_height="wrap_content"



android:text="@string/ok"



android:textColor="@android:color/black"



android:textSize="20px"  />





(2)在res\layout文件夹中创建布局文件secondactivity_layout.xml。在布局文件中,增加文本框控件来显示用户输入的信息,并修改其默认属性。修改完成后布局代码如下所示:








android:layout_width="match_parent"



android:layout_height="match_parent"



android:background="@drawable/background"



android:orientation="vertical"  >






android:id="@+id/usr"