大家好,又见面了,我是全栈君。
之前已经有过两篇写Widget的了,实例一和实例二,如果乍看这篇有难度,可以先看看前两个比较简单的.
本篇完成项目是一个小的心情记录器
整体效果图如下:
这里我们是通过点击桌面的widget弹出记录心情的activity的,
实现方法如下:
Intent intentClick = new Intent(context, update.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intentClick, 0);
rv.setOnClickPendingIntent(R.id.layout, pendingIntent);
下面是通过点击打开 Activity 操作界面的效果图
如何通过点击保存的与app Widget 做动态交互呢?来看下面这段代码
RemoteViews views = new RemoteViews(update.this
.getPackageName(), R.layout.main);
views.setTextViewText(R.id.TextView01, text);
views.setImageViewResource(R.id.ImageView01, util.image[index]);
ComponentName widget = new ComponentName(update.this,
widgetProvider.class);
AppWidgetManager manager = AppWidgetManager
.getInstance(update.this);
manager.updateAppWidget(widget, views);
这里同样还是用到发RemoteViews 来接收值的变化,然后通过AppWidgetManager 这个桌面组件管理器去改新RemoteViews 。由于我们要时时刻保存用户记录的数据,这里只是用到了键值对保存。前两篇已经讲得知识点差不多了,所以这里就不仔细讲解知识点了,下面贴上代码:
package com.terry;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class update extends Activity {
private EditText mEditText;
private Button mButton;
private Spinner mSpinner;
private int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
mEditText = (EditText) findViewById(R.id.EditText01);
mButton = (Button) findViewById(R.id.Button01);
mSpinner = (Spinner) findViewById(R.id.Spinner01);
final ImageView iv = (ImageView) findViewById(R.id.ImageView01);
ArrayAdapter<String> adpter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, util.text);
adpter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adpter);
SharedPreferences settings = getSharedPreferences("settinginfo",
Activity.MODE_PRIVATE);
index = settings.getInt("imageState", 0);
mEditText.setText(settings.getString("heart", ""));
iv.setImageResource(util.image[index]);
mSpinner.setSelection(index);
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
index = arg2;
iv.setImageResource(util.image[index]);
}
@Override
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub
}
});
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = mEditText.getText().toString();
if (text.equals("")) {
return;
}
SharedPreferences shared = getSharedPreferences("settinginfo",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putInt("imageState", index);
editor.putString("heart", text);
editor.commit();
RemoteViews views = new RemoteViews(update.this
.getPackageName(), R.layout.main);
views.setTextViewText(R.id.TextView01, text);
views.setImageViewResource(R.id.ImageView01, util.image[index]);
ComponentName widget = new ComponentName(update.this,
widgetProvider.class);
AppWidgetManager manager = AppWidgetManager
.getInstance(update.this);
manager.updateAppWidget(widget, views);
update.this.finish();
}
});
}
}
由于组件每创建一次都调用了一次updateAppWidget 这个方法,故此方法也必须去获取键值对
package com.terry;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.RemoteViews;
import android.widget.Toast;
public class widgetProvider extends AppWidgetProvider {
private static RemoteViews rv;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
public static void updateAppWidget(Context context,
AppWidgetManager appWidgeManger, int appWidgetId) {
rv = new RemoteViews(context.getPackageName(), R.layout.main);
SharedPreferences shared = context.getSharedPreferences("settinginfo",
Activity.MODE_PRIVATE);
// util.index = settings.getInt("imageState", 0);
// mEditText.setText(settings.getString("heart", ""));
rv.setTextViewText(R.id.TextView01, shared.getString("heart", context
.getResources().getString(R.string.load)));
rv.setImageViewResource(R.id.ImageView01, util.image[shared.getInt(
"imageState", 0)]);
Intent intentClick = new Intent(context, update.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intentClick, 0);
rv.setOnClickPendingIntent(R.id.layout, pendingIntent);
appWidgeManger.updateAppWidget(appWidgetId, rv);
}
}
里面有用到一些图片资源,如果对项目有兴趣的话,
可以去下载整个项目的源码:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/121413.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...