• 一个小控件,可写签名,适合喜欢美观的童鞋


    喜欢的童鞋点击下载安装:http://www.apkbus.com/android-95651-1-1.html

    可以添加签名:

    主程序:

     1 package com.bn.ex12j;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 import android.view.View;
     7 import android.view.View.OnClickListener;
     8 import android.widget.Button;
     9 import android.widget.EditText;
    10 import android.widget.Toast; 
    11 
    12 public class Sample12_10_Activity extends Activity 
    13 {
    14     EditText et;
    15     
    16     @Override
    17     public void onCreate(Bundle savedInstanceState) 
    18     {
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.main);
    21         
    22         //初始化指向输入心情的文本区的引用
    23         et=(EditText)this.findViewById(R.id.EditText01);
    24         //获取确定按钮
    25         Button b=(Button)this.findViewById(R.id.Button01);
    26         //给确定按钮添加监听器
    27         b.setOnClickListener
    28         (
    29             new OnClickListener() 
    30             {
    31                 @Override
    32                 public void onClick(View v) 
    33                 {
    34                     //获取文本框中输入的心情
    35                     String msg=et.getText().toString();
    36                     if(msg.trim().length()==0)
    37                     {//若输入的心情为空则提示并返回
    38                         Toast.makeText
    39                         (
    40                                 Sample12_10_Activity.this, 
    41                                 "心情不能为空!!!", 
    42                                 Toast.LENGTH_SHORT
    43                         ).show();
    44                         return;
    45                     }
    46                     else if(msg.length()>12)
    47                     {//若输入的心情超过长度则提示并返回
    48                         Toast.makeText
    49                         (
    50                                 Sample12_10_Activity.this, 
    51                                 "心情不能大于12个字!!!", 
    52                                 Toast.LENGTH_SHORT
    53                         ).show();
    54                         return;
    55                     }
    56                     
    57                     //若输入合法则发送Intent修改widget中的内容
    58                     Intent intent = new Intent("wyf.action.update_xq");
    59                     intent.putExtra("xxq", msg);
    60                     Sample12_10_Activity.this.sendBroadcast(intent);    
    61                     //发送完Intent结束Activity
    62                     Sample12_10_Activity.this.finish();
    63                 }                
    64             }
    65         );
    66     }
    67 }

    另外java包两个:

      1 package com.bn.ex12j;
      2 
      3 import android.app.PendingIntent;
      4 import android.appwidget.AppWidgetManager;
      5 import android.appwidget.AppWidgetProvider;
      6 import android.content.ComponentName;
      7 import android.content.Context;
      8 import android.content.Intent;
      9 import android.content.SharedPreferences;
     10 import android.util.Log;
     11 import android.widget.RemoteViews;
     12 
     13 //特别注意此类对象每次收到消息后系统会造一个新对象,因此此类对象不可用与存储状态
     14 public class MyWidgetProvider  extends AppWidgetProvider
     15 {
     16     RemoteViews rv; 
     17 
     18     public MyWidgetProvider()
     19     {
     20         Log.d("MyWidgetProvider","============");    
     21     }
     22     
     23     @Override  
     24     public void onDisabled(Context context) 
     25     {//若为最后一个实例
     26         //删除时停止后台定时更新Widget时间的Service
     27         context.stopService(new Intent(context,TimeService.class));
     28     }
     29     
     30     @Override  
     31     public void onEnabled (Context context) 
     32     {//若为第一个实例则打开服务
     33         //启动后台定时更新时间的Service
     34         context.startService(new Intent(context,TimeService.class));             
     35     }
     36     
     37     //onUpdate为组件在桌面上生成时调用,并更新组件UI
     38     @Override
     39     public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) 
     40     {
     41         //创建RemoteViews
     42         rv = new RemoteViews(context.getPackageName(), R.layout.wmain);    
     43         //创建启动修改心情的Activity的Intent
     44         Intent intent = new Intent(context,Sample12_10_Activity.class);
     45         //创建包裹此Intent的PendingIntent
     46         PendingIntent pendingIntent=PendingIntent.getActivity
     47         (
     48                 context, 
     49                 0, 
     50                 intent, 
     51                 PendingIntent.FLAG_UPDATE_CURRENT
     52         );
     53         //设置按下Widget中文本框发送此PendingIntent
     54         rv.setOnClickPendingIntent(R.id.TextView01, pendingIntent);          
     55         
     56         //获取SharedPreferences
     57         SharedPreferences sp=context.getSharedPreferences("xqsj", Context.MODE_PRIVATE);
     58         //从SharedPreferences中读取上次的心情
     59         String xqStr=sp.getString
     60         (
     61                 "xq",   //键值
     62                 null    //默认值
     63         );
     64         if(xqStr!=null)
     65         {//若上次心情存在则更新心情
     66             rv.setTextViewText(R.id.TextView01, xqStr);
     67         }
     68         
     69         //更新Widget
     70         appWidgetManager.updateAppWidget(appWidgetIds, rv);
     71     }
     72     
     73     @Override  //onReceiver 为接收广播时调用更新UI
     74     public void onReceive(Context context, Intent intent) 
     75     {
     76         super.onReceive(context, intent);
     77         if (rv == null) 
     78         {
     79             //创建RemoteViews
     80             rv = new RemoteViews(context.getPackageName(), R.layout.wmain);
     81         }
     82         if (intent.getAction().equals("wyf.action.update_xq")) 
     83         {//收到的是更新心情的 Action则更新心情
     84             //更新心情
     85             rv.setTextViewText(R.id.TextView01, intent.getStringExtra("xxq"));            
     86             //向Preferences中写入心情
     87             SharedPreferences sp=context.getSharedPreferences("xqsj", Context.MODE_PRIVATE);
     88             SharedPreferences.Editor editor=sp.edit();
     89             editor.putString("xq",intent.getStringExtra("xxq"));
     90             editor.commit();
     91         }
     92         else  if (intent.getAction().equals("wyf.action.time_upadte")) 
     93         {//收到的是更新时间的 Action则更新时间
     94             rv.setTextViewText(R.id.TextView02,intent.getStringExtra("time"));
     95         }     
     96         else  if (intent.getAction().equals("wyf.action.load_xq")) 
     97         {//收到的更新心情并添加监听则更新心情并添加监听==为切屏(横竖屏切换)服务
     98             //创建启动修改心情的Activity的Intent
     99             Intent intentTemp = new Intent(context,Sample12_10_Activity.class);
    100             //创建包裹此Intent的PendingIntent
    101             PendingIntent pendingIntent=PendingIntent.getActivity
    102             (
    103                     context, 
    104                     0, 
    105                     intentTemp,
    106                     PendingIntent.FLAG_UPDATE_CURRENT
    107             );
    108             //设置按下Widget中文本框发送此PendingIntent
    109             rv.setOnClickPendingIntent(R.id.TextView01, pendingIntent);  
    110             
    111             //获取SharedPreferences
    112             SharedPreferences sp=context.getSharedPreferences("xqsj", Context.MODE_PRIVATE);
    113             //从SharedPreferences中读取上次的心情
    114             String xqStr=sp.getString
    115             (
    116                     "xq",   //键值
    117                     null    //默认值
    118             );
    119             if(xqStr!=null)
    120             {//若上次心情存在则更新心情
    121                 rv.setTextViewText(R.id.TextView01, xqStr);
    122             }
    123         }    
    124 
    125         //真正更新Widget
    126         AppWidgetManager appWidgetManger = AppWidgetManager.getInstance(context);
    127         int[] appIds = appWidgetManger.getAppWidgetIds
    128         (
    129             new ComponentName
    130             (
    131                 context, 
    132                 MyWidgetProvider.class
    133             )
    134         );
    135         appWidgetManger.updateAppWidget(appIds, rv);
    136     }
    137 }
     1 package com.bn.ex12j;
     2 
     3 import java.util.Date;
     4 
     5 import android.app.Service;
     6 import android.content.Intent;
     7 import android.os.IBinder;
     8 
     9 public class TimeService extends Service 
    10 {
    11     boolean flag=true;//线程循环标志
    12     Thread task;//定时刷新时间的任务线程
    13     
    14     @Override
    15     public IBinder onBind(Intent arg0) 
    16     {
    17         //因为本例用不到Bind功能,因此直接返回null
    18         return null;
    19     }
    20    
    21     @Override
    22     public void onCreate()
    23     {
    24         super.onCreate();
    25         //创建定时更新时间的任务线程
    26         task=new Thread()
    27         {
    28             public void run()
    29             {
    30                 while(flag)
    31                 {
    32                     //定时发送Intent更新时间
    33                     Intent intent = new Intent("wyf.action.time_upadte");
    34                     Date d=new Date();
    35                     StringBuilder sb=new StringBuilder();
    36                     sb.append(d.getYear()+1900);
    37                     sb.append("年");
    38                     sb.append((d.getMonth()+1<10)?"0":"");
    39                     sb.append(d.getMonth()+1);
    40                     sb.append("月");
    41                     sb.append((d.getDate()<10)?"0":"");
    42                     sb.append(d.getDate());
    43                     sb.append("日  ");
    44                     sb.append((d.getHours()<10)?"0":"");
    45                     sb.append(d.getHours());
    46                     sb.append(":");
    47                     sb.append((d.getMinutes()<10)?"0":"");
    48                     sb.append(d.getMinutes());
    49                     sb.append(":");
    50                     sb.append((d.getSeconds()<10)?"0":"");
    51                     sb.append(d.getSeconds());
    52                     
    53                     intent.putExtra("time", sb.toString());
    54                     TimeService.this.sendBroadcast(intent);    
    55                     
    56                     //定时发送Intent更新心情内容,并给心情文本View添加监听器
    57                     //防止切屏后widget不工作了
    58                     intent = new Intent("wyf.action.load_xq");
    59                     TimeService.this.sendBroadcast(intent);    
    60                     
    61                     try 
    62                     {
    63                         Thread.sleep(500);
    64                     } catch (InterruptedException e) 
    65                     {
    66                         e.printStackTrace();
    67                     }
    68                 }
    69             }
    70         };
    71     }
    72     
    73     @Override
    74     public void onStart(Intent intent, int id)
    75     {        
    76         //启动任务线程
    77         task.start();
    78     }
    79     
    80     @Override
    81     public void onDestroy()
    82     {
    83         //关闭定时更新时间的任务线程
    84         flag=false;
    85     }
    86 }

    main。xml文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     android:layout_width="fill_parent"
     5     android:layout_height="fill_parent"
     6     >
     7   <TextView 
     8     android:text="今日心情"
     9     android:id="@+id/TextView01" 
    10     android:layout_width="wrap_content" 
    11     android:layout_height="wrap_content"
    12     android:textColor="#FFFFFF"
    13     android:textSize="20dip"
    14     android:clickable="true"
    15   >
    16   </TextView>
    17   <EditText 
    18     android:id="@+id/EditText01" 
    19     android:singleLine="false"
    20     android:layout_width="fill_parent" 
    21     android:layout_height="60dip"
    22   >
    23   </EditText>  
    24   <Button 
    25     android:text="确定" 
    26     android:id="@+id/Button01" 
    27     android:layout_width="wrap_content" 
    28     android:layout_height="wrap_content">
    29   </Button>
    30 </LinearLayout>

    wmain.xml文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     android:layout_width="fill_parent"
     5     android:layout_height="fill_parent"
     6     android:background="@drawable/dialog"
     7     >
     8   <TextView 
     9     android:text="请点击输入心情"
    10     android:id="@+id/TextView01" 
    11     android:layout_width="fill_parent" 
    12     android:layout_height="wrap_content"
    13     android:textColor="#FFFFFF"
    14     android:textSize="24dip"
    15     android:clickable="true"
    16   >
    17   </TextView>
    18   <TextView
    19     android:id="@+id/TextView02" 
    20     android:layout_width="fill_parent" 
    21     android:layout_height="wrap_content"
    22     android:textColor="#FFFFFF"
    23     android:textSize="24dip"    
    24   >
    25   </TextView>
    26 </LinearLayout>

    mainfest文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3       package="com.bn.ex12j"
     4       android:versionCode="1"
     5       android:versionName="1.0">
     6     <application android:icon="@drawable/icon" android:label="@string/app_name">
     7         <activity android:name=".Sample12_10_Activity"
     8                   android:label="@string/app_name">
     9             <intent-filter>
    10                 <action android:name="android.intent.action.MAIN" />
    11                 <category android:name="android.intent.category.LAUNCHER" />
    12             </intent-filter>
    13         </activity>
    14     <receiver 
    15           android:name=".MyWidgetProvider"
    16           android:label="时间心情"
    17           android:icon="@drawable/heart"
    18         >
    19             <meta-data android:name="android.appwidget.provider"
    20                 android:resource="@xml/appwidgetprovder"></meta-data>
    21             <intent-filter>
    22                 <action android:name="wyf.action.time_upadte"></action>
    23                 <action android:name="wyf.action.update_xq"></action>                
    24                 <action android:name="wyf.action.load_xq"></action>
    25                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />                 
    26             </intent-filter>
    27         </receiver>
    28         <service android:name=".TimeService" android:process=":remote"/>
    29     </application>
    30     <uses-sdk android:minSdkVersion="7" />
    31 
    32 </manifest> 
  • 相关阅读:
    我的第一个Python爬虫——谈心得
    python写第一个网页
    科学计算和可视化
    类和正则表达(自动更正,和代数运算)
    图片处理
    jieba
    汉诺塔问题
    Python核心编程(第二版)第十章习题答案
    python官方文档之open()函数的翻译
    python打开文件时提示“File was loaded in the wrong encoding:’UTF-8”根因调查
  • 原文地址:https://www.cnblogs.com/xmb7/p/2907464.html
Copyright © 2020-2023  润新知