Android编程Widget创建与使用方法简明教程

 更新时间:2016年10月27日 09:31:45   作者:Wallace  
这篇文章主要介绍了Android编程Widget创建与使用方法,结合实例形式分析了Widget的功能、使用方法与相关注意事项,需要的朋友可以参考下

本文实例讲述了Android编程Widget创建与使用方法。分享给大家供大家参考,具体如下:

Android reference中有关于如何建立一个Widget的详细方法,这里简要说明一下,详情可以查看Android SDK中自带的reference。

要建立一个Widget,分为如下几个步骤:

(1) 创建一个类,让其继承类AppWidgetProvider,在AppWidgetProvider中有许多方法,例如onDelete(Context,int[]),onEnable(Context)等,但一般情况下我们只是覆写onUpdate(Context,AppWidgetManager,int[])方法。在该方法中,我们启动后台服务的类,一般是启动Thread类或者Android中的Service类。在该类中我们进行从服务器端获得数据并进行处理并在Widget中显示。

(2) 在你的AndroidMenifest.xml中添加一个receiver标签,让其指向你的AppWidgetProvider子类。内容如下:

<receiver android:name="JiwaiWidget"
android:label="@string/app_name"
android:icon="@drawable/jiwai">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
      android:resource="@xml/info" />
</receiver>

对上面的代码进行解释:

第一行指定该Widget的接收者是JiwaiWidget,即你建立的AppWidgetProvider子类;

第二行指定该Widget的标签名称,值为value目录下string.xml中的app_name值;

第三行指定该Widget的图标,值为drawable目录下jiwai图片;

第四行-第六行是采用Android文档中提供的;

第七行指定该Widget的描述者信息,该描述着中定义了Widget的相关信息,如该Widget的宽度、长度、自动更新的间隔时间等信息,该描述位于xml目录下的info.xml中。

(3) 编写你的Widget的provider文件信息(本例中是xml/info.xml)

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="200dp"
  android:minHeight="90dp"
  android:updatePeriodMillis="43200000"
  android:initialLayout="@layout/appwidget"
  android:configure="com.lawrenst.jiwai.JiwaiConfigure">
</appwidget-provider>

其中android:updatePeriodMillis是自动更新的时间间隔,android:initialLayout是Widget的界面描述文件。Android:configure是可选的,如果你的Widget需要在启动时先启动一个Activity,则需要设定该项为你的Activity。本例中,需要你的嘀咕帐号和密码,所以应先显示一个Activity,输入你的账号和密码,然后将得到的信息在你的Widget中显示。

(4) 在layout目录下编写appwidget.xml文件,配置你的Widget的界面信息:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/widget"
android:background="@drawable/title_a">
<LinearLayout android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:background="@drawable/title">
<TextView android:id="@+id/username_display"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:textSize="15px"
android:gravity="left|center_vertical"
android:paddingLeft="6px" />
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/text1"
android:layout_width="fill_parent"
android:textColor="#ffffff"
android:textSize="12px"
android:gravity="center_vertical|left"
android:paddingLeft="6px"
android:layout_height="30px">
</TextView>
<TextView android:id="@+id/text2"
android:textColor="#ffffff"
android:layout_height="30px"
android:gravity="center_vertical|left"
android:textSize="12px"
android:paddingLeft="6px"
android:layout_width="fill_parent">
</TextView>
</LinearLayout>
</LinearLayout>

该Widget中包括三个Textview,两个用来显示叽歪的信息,一个用来显示用户名,上述代码比较简单,故不做解释。

(5) 由于需要一个Acvivity对象用来输入账户信息,所以在layout目录下新建一个login.xml,作为Activity的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="#ff8c00"
android:capitalize="characters"
android:textStyle="bold" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user"
android:textColor="#ff8cff"
android:capitalize="characters" />
<EditText android:id="@+id/username"
android:layout_width="200px"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/code"
android:textColor="#ff8cff"
android:capitalize="characters" />
<EditText android:id="@+id/password"
android:layout_width="200px"
android:layout_height="wrap_content"
android:password="true" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
  android:id="@+id/submit"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Submit" 
  />
</LinearLayout>
</LinearLayout>

有两个EditText用来输入用户名和密码,另外还有一个Button对象。
准备工作差不多了,下面就可以写代码了。

这里再分享一个案例供大家参考:https://www.jb51.net/books/40184.html

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android基本组件用法总结》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

最新评论