Android高手进阶教程(八)之----Android Widget开发案例(世界杯倒计时!) 2010-04-29 21:11:25 标签: Android 世界杯 Widget android教程 移动开发 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处、作者信息和本声明。否则将追究法律责任。 http://weizhulin.blog.51cto.com/1556324/311438 今天我们要写一下Android Widget的开发,由于快点凌晨,我就不说的太具体了,同志们就模仿吧!首先看一下效果图: 下面是Demo的详细步骤: 一、新建一个Android工程命名为:WidgetDemo. 二、准备素材,一个是Widget的图标,一个是Widget的背景。存放目录如下图: 三、修改string.xml文件如下:
            
  1. viewplaincopytoclipboardprint?
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <resources>
  4. <stringname="hello">HelloWorld,WidetDemo!</string>
  5. <stringname="app_name">DaysToWorldCup</string>
  6. </resources>
  7. <?xmlversion="1.0"encoding="utf-8"?>
  8. <resources>
  9. <stringname="hello">HelloWorld,WidetDemo!</string>
  10. <stringname="app_name">DaysToWorldCup</string>
  11. </resources>
四、建立Widget内容提供者文件,我们在res下建立xml文件夹,并且新建一个widget_provider.xml代码入下:
            
  1. viewplaincopytoclipboardprint?
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"
  4. android:minWidth="50dip"
  5. android:minHeight="50dip"
  6. android:updatePeriodMillis="10000"
  7. android:initialLayout="@layout/main"
  8. />
  9. <?xmlversion="1.0"encoding="utf-8"?>
  10. <appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"
  11. android:minWidth="50dip"
  12. android:minHeight="50dip"
  13. android:updatePeriodMillis="10000"
  14. android:initialLayout="@layout/main"
  15. />
五、修改main.xml布局,代码如下:
            
  1. viewplaincopytoclipboardprint?
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:background="@drawable/wordcup"
  8. >
  9. <TextView
  10. android:id="@+id/wordcup"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello"
  14. android:textSize="12px"
  15. android:textColor="#ff0000"
  16. />
  17. </LinearLayout>
  18. <?xmlversion="1.0"encoding="utf-8"?>
  19. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  20. android:orientation="vertical"
  21. android:layout_width="fill_parent"
  22. android:layout_height="fill_parent"
  23. android:background="@drawable/wordcup"
  24. >
  25. <TextView
  26. android:id="@+id/wordcup"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:text="@string/hello"
  30. android:textSize="12px"
  31. android:textColor="#ff0000"
  32. />
  33. </LinearLayout>
  34. 六、修改WidgetDemo.java代码如下:
  35. viewplaincopytoclipboardprint?
  36. packagecom.android.tutor;
  37. importjava.util.Calendar;
  38. importjava.util.Date;
  39. importjava.util.GregorianCalendar;
  40. importjava.util.Timer;
  41. importjava.util.TimerTask;
  42. importandroid.appwidget.AppWidgetManager;
  43. importandroid.appwidget.AppWidgetProvider;
  44. importandroid.content.ComponentName;
  45. importandroid.content.Context;
  46. importandroid.widget.RemoteViews;
  47. publicclassWidetDemoextendsAppWidgetProvider{
  48. /**Calledwhentheactivityisfirstcreated.*/
  49. @Override
  50. publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,
  51. int[]appWidgetIds){
  52. Timertimer=newTimer();
  53. timer.scheduleAtFixedRate(newMyTime(context,appWidgetManager),1,60000);
  54. super.onUpdate(context,appWidgetManager,appWidgetIds);
  55. }
  56. privateclassMyTimeextendsTimerTask{
  57. RemoteViewsremoteViews;
  58. AppWidgetManagerappWidgetManager;
  59. ComponentNamethisWidget;
  60. publicMyTime(Contextcontext,AppWidgetManagerappWidgetManager){
  61. this.appWidgetManager=appWidgetManager;
  62. remoteViews=newRemoteViews(context.getPackageName(),R.layout.main);
  63. thisWidget=newComponentName(context,WidetDemo.class);
  64. }
  65. publicvoidrun(){
  66. Datedate=newDate();
  67. Calendarcalendar=newGregorianCalendar(2010,06,11);
  68. longdays=(((calendar.getTimeInMillis()-date.getTime())/1000))/86400;
  69. remoteViews.setTextViewText(R.id.wordcup,"距离南非世界杯还有"+days+"天");
  70. appWidgetManager.updateAppWidget(thisWidget,remoteViews);
  71. }
  72. }
  73. }
  74. packagecom.android.tutor;
  75. importjava.util.Calendar;
  76. importjava.util.Date;
  77. importjava.util.GregorianCalendar;
  78. importjava.util.Timer;
  79. importjava.util.TimerTask;
  80. importandroid.appwidget.AppWidgetManager;
  81. importandroid.appwidget.AppWidgetProvider;
  82. importandroid.content.ComponentName;
  83. importandroid.content.Context;
  84. importandroid.widget.RemoteViews;
  85. publicclassWidetDemoextendsAppWidgetProvider{
  86. /**Calledwhentheactivityisfirstcreated.*/
  87. @Override
  88. publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,
  89. int[]appWidgetIds){
  90. Timertimer=newTimer();
  91. timer.scheduleAtFixedRate(newMyTime(context,appWidgetManager),1,60000);
  92. super.onUpdate(context,appWidgetManager,appWidgetIds);
  93. }
  94. privateclassMyTimeextendsTimerTask{
  95. RemoteViewsremoteViews;
  96. AppWidgetManagerappWidgetManager;
  97. ComponentNamethisWidget;
  98. publicMyTime(Contextcontext,AppWidgetManagerappWidgetManager){
  99. this.appWidgetManager=appWidgetManager;
  100. remoteViews=newRemoteViews(context.getPackageName(),R.layout.main);
  101. thisWidget=newComponentName(context,WidetDemo.class);
  102. }
  103. publicvoidrun(){
  104. Datedate=newDate();
  105. Calendarcalendar=newGregorianCalendar(2010,06,11);
  106. longdays=(((calendar.getTimeInMillis()-date.getTime())/1000))/86400;
  107. remoteViews.setTextViewText(R.id.wordcup,"距离南非世界杯还有"+days+"天");
  108. appWidgetManager.updateAppWidget(thisWidget,remoteViews);
  109. }
  110. }
  111. }
七、修改配置文件AndroidManifest.xml,代码如下:
            
  1. viewplaincopytoclipboardprint?
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  4. package="com.android.tutor"
  5. android:versionCode="1"
  6. android:versionName="1.0">
  7. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  8. <receiverandroid:name=".WidetDemo"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"/>
  12. </intent-filter>
  13. <meta-dataandroid:name="android.appwidget.provider"
  14. android:resource="@xml/widget_provider"
  15. />
  16. </receiver>
  17. </application>
  18. <uses-sdkandroid:minSdkVersion="7"/>
  19. </manifest>
  20. <?xmlversion="1.0"encoding="utf-8"?>
  21. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  22. package="com.android.tutor"
  23. android:versionCode="1"
  24. android:versionName="1.0">
  25. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  26. <receiverandroid:name=".WidetDemo"
  27. android:label="@string/app_name">
  28. <intent-filter>
  29. <actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"/>
  30. </intent-filter>
  31. <meta-dataandroid:name="android.appwidget.provider"
  32. android:resource="@xml/widget_provider"
  33. />
  34. </receiver>
  35. </application>
  36. <uses-sdkandroid:minSdkVersion="7"/>
  37. </manifest>
八、点击运行(Ctrl+F11),之,运行成功后,我们长时间点击桌面,会出现如下俩个,依次点击,就可以看到最上面的效果图: 今天就到这里了,我困了呵呵,我发现时间好像不对劲,lol~我也不去多想了,大家知道的告诉我下!对日历这些东西不是太了解,谢谢!! Android高手进阶教程(五)之----Android 中LayoutInflater的使用! 2010-04-21 21:11:25 标签: android开发 Android Layoutinflater android教程 移动开发 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处、作者信息和本声明。否则将追究法律责任。 http://weizhulin.blog.51cto.com/1556324/311450 大家好我们这一节讲的是LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(), 不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。 为了让大家容易理解我做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。 效果图如下: 下面我将详细的说明Demo的实现过程: 1、新建一个 Android工程,我们命名为LayoutInflaterDemo. 2、修改main.xml布局,里面主要在原来基础上增加了一个Button.代码如下:
                    
  1. viewplaincopytoclipboardprint?
  2. <?xmlversion="1.0"
  3. encoding="utf-8"?>
  4. <LinearLayout
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:orientation="vertical"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. >
  10. <TextView
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello"
  14. />
  15. <Button
  16. android:id="@+id/button"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="ShowCustomDialog"
  20. />
  21. </LinearLayout>
  22. <?xmlversion="1.0"
  23. encoding="utf-8"?>
  24. <LinearLayout
  25. xmlns:android="http://schemas.android.com/apk/res/android"
  26. android:orientation="vertical"
  27. android:layout_width="fill_parent"
  28. android:layout_height="fill_parent"
  29. >
  30. <TextView
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. android:text="@string/hello"
  34. />
  35. <Button
  36. android:id="@+id/button"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:text="ShowCustomDialog"
  40. />
  41. </LinearLayout>
3.定义对话框的布局方式,我们在layout目录下,新建一个名为 custom_dialog.xml文件具体代码如下:
                    
  1. viewplaincopytoclipboardprint?
  2. <?xmlversion="1.0"
  3. encoding="utf-8"?>
  4. <LinearLayout
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:orientation="horizontal"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:padding="10dp"
  10. >
  11. <ImageViewandroid:id="@+id/image"
  12. android:layout_width="wrap_content"
  13. android:layout_height="fill_parent"
  14. android:layout_marginRight="10dp"
  15. />
  16. <TextViewandroid:id="@+id/text"
  17. android:layout_width="wrap_content"
  18. android:layout_height="fill_parent"
  19. android:textColor="#FFF"
  20. />
  21. </LinearLayout>
  22. <?xmlversion="1.0"
  23. encoding="utf-8"?>
  24. <LinearLayout
  25. xmlns:android="http://schemas.android.com/apk/res/android"
  26. android:orientation="horizontal"
  27. android:layout_width="fill_parent"
  28. android:layout_height="fill_parent"
  29. android:padding="10dp"
  30. >
  31. <ImageViewandroid:id="@+id/image"
  32. android:layout_width="wrap_content"
  33. android:layout_height="fill_parent"
  34. android:layout_marginRight="10dp"
  35. />
  36. <TextViewandroid:id="@+id/text"
  37. android:layout_width="wrap_content"
  38. android:layout_height="fill_parent"
  39. android:textColor="#FFF"
  40. />
  41. </LinearLayout>
4.修改主程序LayouInflaterDemo.java代码如下:
                    
  1. viewplaincopytoclipboardprint?
  2. packagecom.android.tutor;
  3. importandroid.app.Activity;
  4. importandroid.app.AlertDialog;
  5. importandroid.content.Context;
  6. importandroid.os.Bundle;
  7. importandroid.view.LayoutInflater;
  8. importandroid.view.View;
  9. importandroid.view.View.OnClickListener;
  10. importandroid.widget.Button;
  11. importandroid.widget.ImageView;
  12. importandroid.widget.TextView;
  13. publicclassLayoutInflaterDemoextendsActivityimplements
  14. OnClickListener{
  15. privateButtonbutton;
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. button=(Button)findViewById(R.id.button);
  20. button.setOnClickListener(this);
  21. }
  22. @Override
  23. publicvoidonClick(Viewv){
  24. showCustomDialog();
  25. }
  26. publicvoidshowCustomDialog()
  27. {
  28. AlertDialog.Builderbuilder;
  29. AlertDialogalertDialog;
  30. ContextmContext=LayoutInflaterDemo.this;
  31. //下面俩种方法都可以
  32. ////LayoutInflaterinflater=getLayoutInflater();
  33. LayoutInflaterinflater=(LayoutInflater)
  34. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  35. Viewlayout=inflater.inflate(R.layout.custom_dialog,null);
  36. TextViewtext=(TextView)layout.findViewById(R.id.text);
  37. text.setText("Hello,WelcometoMrWei'sblog!");
  38. ImageViewimage=(ImageView)layout.findViewById(R.id.image);
  39. image.setImageResource(R.drawable.icon);
  40. builder=newAlertDialog.Builder(mContext);
  41. builder.setView(layout);
  42. alertDialog=builder.create();
  43. alertDialog.show();
  44. }
  45. }
  46. packagecom.android.tutor;
  47. importandroid.app.Activity;
  48. importandroid.app.AlertDialog;
  49. importandroid.content.Context;
  50. importandroid.os.Bundle;
  51. importandroid.view.LayoutInflater;
  52. importandroid.view.View;
  53. importandroid.view.View.OnClickListener;
  54. importandroid.widget.Button;
  55. importandroid.widget.ImageView;
  56. importandroid.widget.TextView;
  57. publicclassLayoutInflaterDemoextendsActivityimplements
  58. OnClickListener{
  59. privateButtonbutton;
  60. publicvoidonCreate(BundlesavedInstanceState){
  61. super.onCreate(savedInstanceState);
  62. setContentView(R.layout.main);
  63. button=(Button)findViewById(R.id.button);
  64. button.setOnClickListener(this);
  65. }
  66. @Override
  67. publicvoidonClick(Viewv){
  68. showCustomDialog();
  69. }
  70. publicvoidshowCustomDialog()
  71. {
  72. AlertDialog.Builderbuilder;
  73. AlertDialogalertDialog;
  74. ContextmContext=LayoutInflaterDemo.this;
  75. //下面俩种方法都可以
  76. ////LayoutInflaterinflater=getLayoutInflater();
  77. LayoutInflaterinflater=(LayoutInflater)
  78. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  79. Viewlayout=inflater.inflate(R.layout.custom_dialog,null);
  80. TextViewtext=(TextView)layout.findViewById(R.id.text);
  81. text.setText("Hello,WelcometoMrWei'sblog!");
  82. ImageViewimage=(ImageView)layout.findViewById(R.id.image);
  83. image.setImageResource(R.drawable.icon);
  84. builder=newAlertDialog.Builder(mContext);
  85. builder.setView(layout);
  86. alertDialog=builder.create();
  87. alertDialog.show();
  88. }
  89. }
5、最后执行之,点击Button,将得到上述效果。 好今天就到此为止,睡觉了,大家有什么不明白的请留言~谢谢!

更多相关文章

  1. Android中JavaScript和Native之间的Bridge
  2. 安卓开发06:布局-线性布局 LinearLayout
  3. android 官网处理图片 代码
  4. android ListView上拉加载更多 下拉刷新功能实现(采用pull-to-ref
  5. android长按弹出对话框
  6. android手机震动代码
  7. Android随笔之标题栏的去除以及自定义
  8. Android(安卓)对话框AlertDialog和AlertDialog.Builder两者的区
  9. 【Android开发】布局管理器-相对布局

随机推荐

  1. Android开发去除标题栏title
  2. Android开发工具总结
  3. Android(安卓)Toast 用法总结
  4. Android底部导航栏之BottomNavigationBar
  5. Android(安卓)OpenGL ES(十):绘制三角形Tri
  6. android 之 SurfaceView使用(桌面弹球)
  7. Android(安卓)CountDownTimer倒计时器的
  8. 第六章 Android常见的UI基础控件(二)
  9. Android(安卓)与J2ME 虚拟机相关资料
  10. 【Android】广播大全 Intent Action 事件