【问题标题】:How to create an Android widget with options?如何创建带有选项的 Android 小部件?
【发布时间】:2016-04-07 21:49:23
【问题描述】:

我正在为我的笔记应用程序开发小部件。

我要存档的类似于 colorNote 小部件:

  1. 选择笔记应用程序小部件

  2. 允许用户选择存储在Sqlite 中的可用笔记列表

  3. 显示带有所选笔记内容的小部件。

我可以显示简单的小部件来显示我想要的简单消息,但我想做的是允许用户从注释列表中选择一个指定的注释作为小部件的内容。我很难找到相关资源,如果您知道某个搜索关键字,请告诉我,我会自己研究。

【问题讨论】:

    标签: java android sqlite widget


    【解决方案1】:

    您可以为您的小部件开发设置功能,该功能将在小部件实例化或单击小部件时显示。要为您的小部件提供选项功能,您必须在您的应用中创建设置活动,其中包含供用户选择的选项的 UI 和包含处理设置选择的行为的活动。

    每次用户保存选择时,您都可以将所选选项应用到小部件实例。详细解释见http://www.zoftino.com/android-widget-example

    【讨论】:

      【解决方案2】:

      我认为 ColorNote 使用的是Widget Configuration Activity。 您可以在第一个链接(这是官方链接)上找到一个有用的示例,或者(为什么不)也可以找到there

      要实现“对话框”样式,您必须像这样设置小部件配置活动:

      <activity
       ...
       android:theme="@android:style/Theme.Dialog"
       android:excludeFromRecents="true" />
      

      以上示例取自this answer

      如果您想了解更多关于小部件设计的信息,请查看there

      示例

      1) 创建 widget_info 并将其放在 res/xml 文件夹中:

      <?xml version="1.0" encoding="utf-8"?>
      <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
          android:configure="com.mycompany.app.widgettest.WidgetConfigureActivity"
          android:initialKeyguardLayout="@layout/widget"
          android:initialLayout="@layout/widget"
          android:minHeight="40dp"
          android:minWidth="40dp"
          android:resizeMode="horizontal|vertical"
          android:updatePeriodMillis="86400000"
          android:widgetCategory="home_screen">
      
      </appwidget-provider>
      

      2) 创建你的 widget 布局(我选择了最简单的):

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#09C"
          android:padding="@dimen/widget_margin">
      
          <TextView
              android:id="@+id/appwidget_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"
              android:layout_margin="8dp"
              android:background="#09C"
              android:contentDescription="@string/appwidget_text"
              android:text="@string/appwidget_text"
              android:textColor="#ffffff"
              android:textSize="24sp"
              android:textStyle="bold|italic" />
      
      </RelativeLayout>
      

      随意赋予它自己的风格。

      3) 创建 widget_configure 活动布局:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:padding="16dp">
      
          <TextView
              android:id="@+id/text_view"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginBottom="8dp"
              android:text="@string/configure" />
      
          <EditText
              android:id="@+id/appwidget_text"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:inputType="text"
              android:layout_below="@+id/text_view"/>
      
          <ListView
              android:id="@+id/list"
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:layout_below="@+id/appwidget_text"
              android:layout_above="@+id/add_button">
          </ListView>
      
          <Button
              android:id="@+id/add_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="8dp"
              android:text="@string/add_widget"
              android:layout_alignParentBottom="true" />
      
      </RelativeLayout>
      

      此外,由您决定样式和编辑活动的布局;在上面的示例中,我添加了一个编辑文本,它将创建一个“新”注释(但它不保存在数据库中)和一个包含虚拟内容的列表。由您来填充您的列表,当然是从您的数据库中获取的。

      4) 将小部件配置活动和小部件提供程序添加到您的清单中(在 application 标记内):

              <receiver android:name=".AppWidget">
                  <intent-filter>
                      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                  </intent-filter>
      
                  <meta-data
                      android:name="android.appwidget.provider"
                      android:resource="@xml/widget_info" />
              </receiver>
      
              <activity
                  android:name=".WidgetConfigureActivity"
                  android:theme="@android:style/Theme.Dialog"
                  android:excludeFromRecents="true">
                  <intent-filter>
                      <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
                  </intent-filter>
              </activity>
      

      5) 创建将扩展 AppWidgetProvider 的类 AppWidget(请注意,应用小部件配置在以下类 WidgetConfigureActivity 中实现和管理)[我没有在该类中添加任何特殊内容]:

      import android.appwidget.AppWidgetProvider;
      import android.appwidget.AppWidgetManager;
      import android.content.Context;
      import android.widget.RemoteViews;
      
      /**
       * Implementation of App Widget functionality.
       */
      public class AppWidget extends AppWidgetProvider {
      
          @Override
          public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
              // There may be multiple widgets active, so update all of them
              for (int appWidgetId : appWidgetIds) {
                  updateAppWidget(context, appWidgetManager, appWidgetId);
              }
          }
      
          @Override
          public void onDeleted(Context context, int[] appWidgetIds) {
              // When the user deletes the widget, delete the preference associated with it.
              for (int appWidgetId : appWidgetIds) {
                  WidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
              }
          }
      
          @Override
          public void onEnabled(Context context) {
              // Enter relevant functionality for when the first widget is created
          }
      
          @Override
          public void onDisabled(Context context) {
              // Enter relevant functionality for when the last widget is disabled
          }
      
          static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                      int appWidgetId) {
      
              CharSequence widgetText = WidgetConfigureActivity.loadTitlePref(context, appWidgetId);
              // Construct the RemoteViews object
              RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
              views.setTextViewText(R.id.appwidget_text, widgetText);
      
              // Instruct the widget manager to update the widget
              appWidgetManager.updateAppWidget(appWidgetId, views);
          }
      }
      

      6) 最后,这是WidgetConfigureActivity

      import android.app.Activity;
      import android.appwidget.AppWidgetManager;
      import android.content.Context;
      import android.content.Intent;
      import android.content.SharedPreferences;
      import android.os.Bundle;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.AdapterView;
      import android.widget.ArrayAdapter;
      import android.widget.EditText;
      import android.widget.ListView;
      
      /**
       * The configuration screen for the {@link AppWidget} com.bepatient.app.widgettest.AppWidget.
       */
      public class WidgetConfigureActivity extends Activity {
      
          int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
          EditText mAppWidgetText;
          private static final String PREFS_NAME = "AppWidget";
          private static final String PREF_PREFIX_KEY = "appwidget";
      
          public WidgetConfigureActivity() {
              super();
          }
      
          @Override
          public void onCreate(Bundle icicle) {
              super.onCreate(icicle);
      
              // Set the result to CANCELED.  This will cause the widget host to cancel
              // out of the widget placement if the user presses the back button.
              setResult(RESULT_CANCELED);
      
              setContentView(R.layout.widget_configure);
              // Set layout size of activity
              getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
              mAppWidgetText = (EditText) findViewById(R.id.appwidget_text);
              findViewById(R.id.add_button).setOnClickListener(mOnClickListener);
              final ListView listView = (ListView) findViewById(R.id.list);
      
              // Defined array values to show in ListView
              String[] values = new String[] { "Don't forget the milk!",
                      "Do not forget to go get the mother-in-law",
                      "Go to the laundry",
                      "Marise number 0123456789"
              };
      
              ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                      android.R.layout.simple_list_item_1, android.R.id.text1, values);
      
              listView.setAdapter(adapter);
      
              // ListView Item Click Listener
              listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                      // Take ListView clicked item value
                      String  widgetText    = (String) listView.getItemAtPosition(position);
                      createWidget(getApplicationContext(), widgetText);
                  }
              });
      
              // Find the widget id from the intent.
              Intent intent = getIntent();
              Bundle extras = intent.getExtras();
              if (extras != null) {
                  mAppWidgetId = extras.getInt(
                          AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
              }
      
              // If this activity was started with an intent without an app widget ID, finish with an error.
              if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
                  finish();
                  return;
              }
      
              mAppWidgetText.setText(loadTitlePref(WidgetConfigureActivity.this, mAppWidgetId));
          }
      
          View.OnClickListener mOnClickListener = new View.OnClickListener() {
              public void onClick(View v) {
                  final Context context = WidgetConfigureActivity.this;
      
                  // When the button is clicked, get text
                  String widgetText = mAppWidgetText.getText().toString();
                  createWidget(context, widgetText);
              }
          };
      
          private void createWidget(Context context, String widgetText) {
              // Store the string locally
              saveTitlePref(context, mAppWidgetId, widgetText);
      
              // It is the responsibility of the configuration activity to update the app widget
              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
              AppWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
      
              // Make sure we pass back the original appWidgetId
              Intent resultValue = new Intent();
              resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
              setResult(RESULT_OK, resultValue);
              finish();
          }
      
          // Write the prefix to the SharedPreferences object for this widget
          static void saveTitlePref(Context context, int appWidgetId, String text) {
              SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
              prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
              prefs.apply();
          }
      
          // Read the prefix from the SharedPreferences object for this widget.
          // If there is no preference saved, get the default from a resource
          static String loadTitlePref(Context context, int appWidgetId) {
              SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
              String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
              if (titleValue != null) {
                  return titleValue;
              } else {
                  return context.getString(R.string.appwidget_text);
              }
          }
      
          static void deleteTitlePref(Context context, int appWidgetId) {
              SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
              prefs.remove(PREF_PREFIX_KEY + appWidgetId);
              prefs.apply();
          }
      }
      

      在本课程中,您必须管理和填充保存在您的应用数据库中的所有笔记。当您单击按钮或项目时,方法 createWidget 将为您完成工作。

      注意下面一行:

      // Set layout size of activity
      getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
      

      这必须在 setContentView() 方法之后调用,否则您的活动将变得非常小。如果你运行这个例子,你会发现配置活动有一个简单的样式,所以你必须根据你的应用程序 UI 创建自己的样式(如果你愿意的话!)。

      【讨论】:

      • @cwfei 我只有我自己的代码,它将创建一个像你这样的简单小部件。我将使用 config 创建一个快速示例。
      • 这就是我想要的,通俗易懂,谢谢:)
      • @cwfei 很高兴为您提供帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 2019-07-30
      • 2015-11-27
      • 2012-09-15
      • 2020-06-06
      相关资源
      最近更新 更多