当前位置:  开发笔记 > 编程语言 > 正文

如何使用选项创建Android小部件?

如何解决《如何使用选项创建Android小部件?》经验,为你挑选了1个好方法。

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

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

    选择笔记应用程序小部件 在此输入图像描述

    允许用户选择存储的可用笔记列表 Sqlite 在此输入图像描述

    显示包含所选备注内容的小部件. 在此输入图像描述

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



1> JJ86..:

我认为ColorNote正在使用Widget配置活动.你可以找到的第一个链接(这是官方的一个)上的有用的例子,或(为什么不)也有.

要实现"对话框"样式,您必须像下面这样设置窗口小部件配置活动:


上面的例子取自这个答案.

如果您想了解更多关于小部件的设计,也看看那里.

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





2)创建您的小部件布局(我采用最简单的一个):



    


随意给自己的风格.

3)创建widget_configure活动布局:




    

    

    
    

    

还有,您可以自定义并编辑活动的布局; 在上面的例子中,我添加了一个编辑文本,它将创建一个"新"注释(但它不保存在数据库中)和一个包含虚拟内容的列表.由您来填充您的列表,当然是从您的数据库中获取.

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

        
            
                
            

            
        

        
            
                
            
        

5)创建AppWidget类,它将扩展AppWidgetProvider(注意在下面的类中实现和管理app widget配置,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 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创建自己的样式(如果需要!).

推荐阅读
携手相约幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有