当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程Widget创建与使用方法简明教程

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了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对象。
准备工作差不多了,下面就可以写代码了。

这里再分享一个案例供大家参考:

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网