掌上书城管理系统外文翻译资料

 2022-11-19 16:36:00

Preferences

Preferences are user-specific settings for an application. Preferences usually consist of some configuration data as well as a user interface to manipulate that data.

From the user interface point of view, preferences can be simple text values, check-boxes, selections from a pull-down menu, or similar items. From a data point of view, preferences are a collection of name-value pairs, also known as key-value or attribute-value pairs. The values are basic data types, such as integers, booleans, and strings.

Our micro-blogging application needs to connect to a specific server in the cloud using specific user account information. For that, Yamba needs to know the username and password for that account as well as the URL of the server itrsquo;s connecting to. This URL is also known as the API root. So, in our case, wersquo;ll have three fields where the user can enter and edit his username, password, and the API root. This data will be stored as strings.

To enable our app to handle user-specific preferences, we need to build a screen to enter the information, Java code to validate and process that information, and some kind of mechanism to store this information.

All this sounds like a lot of work, but Android provides a framework to help streamline working with user preferences. First, wersquo;ll define what our preference data looks like in a Preference resource file.

To create preferences for our application, we need to:

  1. Create a Preference resource file called prefs.xml.
  2. Implement the PrefsActivity.java file that inflates that resource file.
  3. Register this new activity with the AndroidManifest.xml file.
  4. Provide a way to start that activity from the rest of the application.

Prefs Resource

We are going to start by creating prefs.xml, a resource file that outlines what our pref-erence screen will look like. The easiest way to create it is to use the New Android XML File tool in Eclipse, . To start the New Android XML File dialog, go to File→New→Android XML File, or click on the little a icon in Eclipsersquo;s top menu bar:

The key is to give the new file a name, in this case prefs.xml, and to choose Preference for the type of resource. The tool should automatically suggest creating this new file in the /res/xml folder and that the root element for the XML file should be PreferenceScreen. we could create alternative versions of this same resource by applying various qualifiers, such as screen size and orientation, language and region, etc.

Wersquo;re using Eclipse tools where applicable to get the job done more quickly. If you were to use another tool, yoursquo;d have to create this file manually and put it in the correct folder.

Once you click on Finish, Eclipse will create a new file for you and open it up. Eclipse typically opens the XML files it knows about in its developer-friendly view.

In this view, you can create the username preference entry by selecting PreferenceScreen on the left, and then choosing Add→EditTextPreference. On the right-hand side, ex-pand the “Attributes from Preferences” section. Eclipse will offer you a number of attributes to set for this EditTextPreference.

Not all attributes are equally important. Typically, you will care about the following:

Key

A unique identifier for each preference item. This is how wersquo;ll look up a particular preference later.

Title

The preference name that the user will see. It should be a short name that fits on a single line of the preference screen.

Summary

A short description of this preference item. This is optional, but using it is highly recommended.

For the username preference, wersquo;ll put “username” for its key. We will define the Title and Summary in strings.xml, as this is the best practice.

Instead of modifying the strings.xml file directly, you can use an Eclipse shortcut. Herersquo;s how it goes:

  1. Click on Browse and select New Stringhellip;. This will open a dialog to create a new string resource.
  2. Enter titleUsername for the R.string. value and Username for the String value.
  3. Click OK, and this will insert a new string resource in strings.xml.
  4. You can now pick that value from the list of resources.

Using these instructions for adding the Username preference item, you can now repeat the same steps for Password and API Root items.

You can switch to the actual XML code by clicking on the tab at the bottom of the window

PrefsActivity

Now that we have the preferences defined in their own XML resource file, we can create the activity to display these preferences. You may recall from lt;lt;Activitiesgt; that every screen in an Android app is an activity. So, to display the screen where a user enters the username and password for his online account, wersquo;ll create an activity to handle that screen. This will be a special preference-aware activity.

To create an activity, we create a new Java class. In Eclipse, select your package under your src folder, right-click on the package, and select New→Class. A New Java Class window will pop up. You just need to enter PrefsActivity for the Name and click Finish. This will create a PrefsActivity.java file under your package in your source folder.

Our PrefsActivity class, shown in Example 7-2, is a very simple Java file. This is be-cause we inherit from PreferenceActivity, an Android framework class that knows how to handle preferences.

1.Unlike regular activities, PrefsActivity will subclass (i.e., extend) the Preference Activity class.

2.Just like any other activity, we override the onCreate() method t

剩余内容已隐藏,支付完成后下载完整资料


首选项

首选项即用户针对某个程序所做的个人设置。它包含一些选项数据,以及一个修改这些选项数据的用户界面。

在用户看来,首选项就是一些文本框,复选框,下拉菜单等类似的东西组成的集合体。 而从数据的角度看来,首选项是一组由键值对(也称为名值对.或者属性/值对)构成的数据集.其中的值都是基本数据类型.如整形、布尔型、字符串等。

我们的微博应用需要连接到云服务端,因此自然要用到用户名与密码.除此之外还需要知道它应该连接到的远程服务器URL (即API地址)。所以在这个例子中,要提供3个选项, 让用户可以输入、修改用户名、密码及API地址。这些数据都将作为字符串存储起来。

为了让用户能在应用上设置首选项.我们需要创建一个可以输入数据的界面.并编写Java 代码来验证和处理数据,还需要某种机制来存储这些信息。

听起来像是工作量不少,不过有Android提供的现成框架可用.我们的工作可以得到大大简化。首先要做的,就是在资源文件中定义首选项的内容。

为应用程序创建首选项的步骤如下。

创建首选项的资源文件prefs,xml

对应这个资源文件,在prefsActivity文件中实现一个活动

将这个新活动注册到Androidmanifest.xml文件。

提供从应用的其他地方启动这个活动的方法。

资源文件

首先从创建prefs.xml开始,这是一个资源文件,用来声明我们的首选项界面大致是什么样子。创建他的最简单的方法就是使用Eclipse的New Android XML File 工具,可以选择file→New→Android XML File,也可以点击菜单栏上的按钮。

这里的重点是要把文件的名字设为prefs.xml,资源类型设为Preference。默认会将文件创建在、rex/xml目录下,XML的默认根节点会设为PreferenceScreen。我们可以为同一资源设罝多个不同的资源文件,以适应不同的屏幕尺寸、旋转方向、语言及区域等参数的需要。

在这里我们通过Eclipse的辅助工具简化了工作。如果使用其他工具.可能就需要手动创建这个文件,并将其置于合适的目录之下。

一旦单击Finish按钮,Eclipse就会创建并且打开一个新文件.由于Eclipse能识别这个XML 文件,因此会在它的可视化设计界面中打开。

在这个界面中.选择左边的PreferenceScreen.然后选择Add→EditTextPreference,就可以为用户名创建对应的选项条目了。展开右边Preferences下边的Attributes.可以见到EditTextPreference诸多属性。

并非所有的属性都同样敢要.一般来说.你会比较关心下面这些。

Key

每个首选项条0的唯一标识符。我们稍后将用这个来杏找特定的首选项。

Title

用户见到的首选项名称。应该起一个比较短的名字,长度不要超过屏幕的一行。

Summary

针对这个选项条B的茼介信息。这是可选的.但是强烈建议提供该信息。

对用户名一项而言.key就足username。至于Title与Summary的值.我们都放在string.xml里面,这是个好习惯。

要编辑string.xml,你也可以避免直接手写,而是使用Eclipse提供的快捷工具。步骤如下:

1.单机Browse按钮.选择New String...,打开新建siring资源的对话框。

2.为R.string输人titleUsername,为String输入Username。

3.单击OK按钮,即可在string.xml中插入一条新的string资源

4.随后就可以在资源列表中取出来引用它了。

以上就是创建Username选项条B的步骤。创建Password及API地址这两项的步骤也类似。 要杳看实际的XML代码.可以单击窗口底下的标签

PrefsActivity 类

对应首选项的XML资源文件已定义完毕,接下来为它们创建一个活动。Android程序中的任何界面都是活动。要为用户提供设罝用户名与密码的界 面.我们需要专门新建一个只负责处理选项数据的特殊活动。

要创建一个活动.首先要创建活动对应的Java类。在Eclipse中的录下,找到你的 package文件夹,右键单机并在弹出的快捷菜中bull;中选抒New→class。在出现的New Java Class窗口中设罝名字为PrefsActivity,单击Finish按钮,就会在你的源代码B录中的 package目录里bull;新建一个PrefsActivity.java文件。

为简化选项界面的开发.Android框架提供丫一个PreferenceActivity类.它已经包含了 处理首选项的逻辑。以它为基类,我们的PrefsActivity类可以很简单

1.与一般的活动不同.PrefsActivity以PrefsActivitity为基类。

2.覆盖onCreate()方法,做些初始化工作.这点同其他活动一样0

3.普通的活动通常会调用setContentView().与之不同的是,这里通过调用 addPreferencesFromResource().装载prefs,xml文件中定义的内容。

在实现或者覆盖方法时,如果不想打字敲出onCreate()等方法冗长的声明,大可使用 Eclipse的工具代劳。假如你在编辑PrefsActivity.java,并且已经加上...extends PreferenceActivity. . .。那就可以选择Source→Ovcrridc/Implcmcnt Methods..然后会出现一个对话框.列出通过父类PreferenceActivity推断出来的,你很可能需要实现或者覆盖的那些方法。在对话框里选择onCreate(), Eclipse就会在你的类定义里面插人这个方法的代码块。

修改Manifest文件

无论何时.只要新建r基本构件(活动.服务.广播接收器,或者内容提供器).就要修 改manifest文件添加相应的声明。在这里,我们需要将新定义的活动 P ref sActivity 注册到 manifest 文件。

同前面一样.在Eclipse中打开文件会默认进人Eclipse提供的阁形化编辑 界面。中it Application选项卡.然后在Application节点之下.选择Add—* Activity bull;输入名 字为.Pref sActivity。

当然也可以通过单击窗口下面的androidmanifest.xml选项卡.直接修改原姶的XML代码。 我个人觉得,Eclipse用来创建新XML文件是很有用的,但编辑原始XML的时候,直接改原代码往往速度更快.而且能够控制更多的东西。

在Eclipse中编辑代码时.可以使用Ctrl-Spacc组合键调出Eclipse的自动完成功能。在输人 XML和Java代码的时候都很有用.而且自动完成功能是基于内容感知的.也就是Eclipse 很智能.河以根据当前代码的上下文.来预测接下来可能输人的内容。使mCtrl-Spacc组 合键能让你的编程生活更加轻松.因为你不用再去i己那些冗长的方法名和标签名了.还 可以避免拼写错误。

选项菜单

选项菜单是一个Android用户界面组件.可以为应用程序提供标准化的菜单。只要用户按下 设备的菜单按钮.它就可以从屏幕下方显示出来。

为程序添加选项菜单,我们需要做如下丁.作。

1.新建一个menu.xml资源表示菜单的内容。

2.在需要显示该菜单的活动中添加方法onCeateOptionsMenu(),在里面装载 menu.xml资源中的内容。

3.在onOptionsItemSelected ()中响应菜单事件0

菜单的资源文件

首先在一个选项菜单的XML资源文件中定义这个菜单。同新建其他Android XML文件的流程一样,单击Eclipse具栏的a 按钮,或者选择File→New→Android XML.打开New Android XML File对话框.在文件一栏输人“menu.xml”,类型选择Menu,然后单击Finish 按钮.Eclipse就会创建一个/res/menu目录,并在这个目录下创建一个新文件menu.xml,同时打开一个图形化的编辑界面

Android系统资源

lt;ID

就像我们的程序都有自己的资源.Android系统也同样拥有。像大多数其他操作系统一 样.Android系统中也内gr许多图标、图像.声音片段等各种类型的资源文件。回想 一下,应用程序的资源文件都在/res/路径下.而在XML中引用Android系统资源.则需 要在路径前面加一个关键词前缀android:.比如@android:drawable/ic_menu_ Preferences。如果是在Java代码中引用Android系统资源.就要用android.R.而不是 通常引用的R。

更新StatusActivity,加载菜单

前面提到,当用户单击设答的菜单按钮的时候.这个选项菜单才会加载到你的活动中。菜 甲按钮第一次被按下时,系统会调用活动的onCreateOptionsMenu()方法.从menu.xml资源文件中解析并加载菜单。

从布局资源中投射用户界面的做法很相似.基本上都是负责解析的程序去读取XML代 码,为其中的每个XML元素建对应的Java对象.并相应地设置每个XML对象的属性。

可以看出.只要活动不被销毁.这个菜中-就会一直存在于内存中.而onCreateOptionsMenu() 也不会被冉次执行。但是,用户每次选择菜单项. onOptionsItemSelected()都会被 调用一次.以处理这次点击。这将在下一节进行讨论。

我们需要更新StatusActivity来加载选项菜单,因此.在StatusActivity中加人一个 onCreateOptionsMenu()方法。这个方法将只在用户第一次按下菜单按钮时被调用。

用户数据分区

对开发者和用户来讲.最m要的分区是用户数据分区。所有的用户数据1所有下载的应用 程序都储存在这里。最重要的是,所有应用程序的数据也都储存在这里.包括预装的程序 和用户自己下载安装的程序。

所以,用户安装的应用程序都储存在/data/app/目录,而我们开发者最关心的数据文件都储存在这个目录之下,每个应用程序对应一个单独的子目录.按照Java package 的名字作为标识。从这里可以再次看出Java package在Android安全机制中的重要作用。

Android框架提供了许多相关的辅助方法.允许应用程序访问用户数据文件系 统,比如 getFilesDir() 这个分区的镜像文件是user-data.img,位于对应设备的AVD目录之下。同前面一样.也是 ~/.android/avd/文件夹之,每个虚拟设备都会有一个对应的子目录

当你新建一个应用程序的时候.你需要为Java代码指定一个特定的package.按约定,它的 名字一般都是逆序的域名加上应用名,比如Yamba应用就对应了com.marakana.yamba 这个package。所以.应用一旦被安装.Android会为其中独创建一个0录 marakana.yamba/。这个目录就是Yamba应用的私有、安全的文件存储位S。

/dala/dala/com.marakana.yamba2/下面也有子目录.但是结构很清晰.不同的数据分列在不 同的0录之下.比如首选项数据就都位于_prefs/Q 实际上.如果你打开Eclipse的DDMS视阁,选择文件浏览器汸问这一B录,就能在里面看 到一个com.marakana.yamba2_preferences.xml文件。你可以把它复制出来查看,也可以在 adb shell中直接查看。

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[23393],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

课题毕业论文、外文翻译、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。