添加几个appWidgets不同的配置? [英] Add several appWidgets with different configuration?

查看:170
本文介绍了添加几个appWidgets不同的配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了它显示一个简单的TextView,这是可编辑的作为EDITTEXT在配置活动的Widget。我保存共享preferences所输入的文字,这样用户可以点击控件编辑文本,和已经输入的文本出现在edittextfield。 我的问题是这样的。我希望用户能够添加多个小部件,但是当seccond小部件添加,相同的文字在其他小部件从共享preferences加载。并且,在插件当编辑,以便为另一个。希望我是清楚的。我还挺有它是与appWidgetIds的想法,但我不能弄明白。

下面是我的code,有点简化。

 公共类WidgetConfig扩展活动实现OnClickListener,OnItemSelectedListener {


    AppWidgetManager AWM;
    诠释AWID;
    语境℃;
    的EditText信息;
    按钮B;
    串音符;
    INT styleStart = -1,cursorLoc = 0;
    共享preferences SP;
    微调微调;
    串[]路径= {10,20,30};
    文件路径= NULL;



    @覆盖
    保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.widgetconfig);

        C = WidgetConfig.this;
        信息=(EditText上)findViewById(R.id.etwidgetconfig);

        ...

        B =(按钮)findViewById(R.id.bwidgetconfig);
        负载preFS();
        b.setOnClickListener(本);

        //获取信息有关发起这项活动的小部件
        意图I = getIntent();
        捆绑额外= i.getExtras();
        如果(临时演员!= NULL){
            AWID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        AWM = AppWidgetManager.getInstance(C);

    }

        ...


    私人无效载荷preFS(){
        SP = preferenceManager.getDefaultShared preferences(本);
        注意= sp.getString(NOTE,默认);

        info.setText(注);

    }

    私人无效节省preFS(字符串键,字符串值){
        SP = preferenceManager.getDefaultShared preferences(本);
        共享preferences.Editor编辑器= sp.edit();
        editor.putString(键,值); //值来存储
        editor.commit();

    }


    公共无效的onClick(视图v){
        // TODO自动生成方法存根

        节省preFS(注意,info.getText()的toString());

        RemoteViews意见=新RemoteViews(c.getPackageName(),R.layout.widget);
        views.setTextViewText(R.id.tvConfigInput,info.getText());

        组件名thisWidget =新的组件名(本,Widget.class);
        AppWidgetManager经理= AppWidgetManager.getInstance(本);
        manager.updateAppWidget(thisWidget,意见);

        意图=新的意图(C,WidgetConfig.class);
        PendingIntent PI = PendingIntent.getActivity(C,0,中,PendingIntent.FLAG_UPDATE_CURRENT);


        views.setOnClickPendingIntent(R.id.B_EditAgain,PI);

        awm.updateAppWidget(AWID,意见);


        意向书结果=新的意图();
        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,AWID);
        的setResult(RESULT_OK,结果);
        完();

    }



}
 

更新:

所以,我想这一点,但是没有什么不同,仍然无法正常工作。

 私人无效载荷preFS(){
        SP = context.getShared preferences(小部件+将String.valueOf(appWidgetId)
          ,Context.MODE_PRIVATE);
        注意= sp.getString(注,);

        info.setText(注);

    }

私人无效节省preFS(字符串键,字符串值){
    SP = context.getShared preferences(小部件+将String.valueOf(appWidgetId)
              ,Context.MODE_PRIVATE);
    编辑EDITOR = sp.edit();
    editor.clear();
    editor.putString(注,info.getText()的toString());
    editor.putString(键,值); //值来存储
    editor.commit();

    }
 

解决方案

您可能不希望使用 getDefaultShared preferences 在这里。所有部件共享相同的默认共享preferences,所以他们会不断相互覆盖。

我在我自己的应用程序同样的情况,所以我用一个自定义的preference文件每个插件。您可以命名与为widgetid的preference文件,然后每个插件总是会得到它自己的一套独特的preferences。

在配置preferenceActivity:

  this.get preferenceManager()。setShared preferencesName(
           小部件+将String.valueOf(mAppWidgetId)
);
 

这将所有的preferenceActivity设置存储到小部件ID命名的preference文件。

然后在小部件本身,只检索对应的ID文件:

  preferences = context.getShared preferences(
      小部件+将String.valueOf(appWidgetId)
      ,Context.MODE_PRIVATE);
 

和检索preferences像正常的。

I've created a widget which displays a simple textview, which is editable as an Edittext in a configuration activity. I save the inputted text with shared preferences, so the user can tap the widget to edit the text, and the already inputted text appears in the edittextfield. My problem is this. I would like the user to be able to add multiple widgets, but when a seccond widget is added, the same text as in the other widget is loaded from shared preferences. And, when on widget is edited so is the other one. Hope i'm being clear. I kinda have an idea it has something to do with appWidgetIds but i cannot figure it out.

Here's my code, a bit simplified.

public class WidgetConfig extends Activity implements OnClickListener, OnItemSelectedListener {


    AppWidgetManager awm;
    int awID;
    Context c;
    EditText info;
    Button b;
    String note;
    int styleStart = -1, cursorLoc = 0;
    SharedPreferences sp;
    Spinner spinner;
    String[] paths = { "10", "20", "30" };
    File path = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widgetconfig);

        c = WidgetConfig.this;
        info = (EditText)findViewById(R.id.etwidgetconfig);

        ...

        b = (Button)findViewById(R.id.bwidgetconfig);
        loadPrefs();
        b.setOnClickListener(this);

        //Getting Info about the widget that launched this activity
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        if (extras != null){
            awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID );
        }

        awm = AppWidgetManager.getInstance(c);

    }

        ...


    private void loadPrefs(){
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        note = sp.getString("NOTE", "DEFAULT");

        info.setText(note);

    }

    private void savePrefs(String key, String value){
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sp.edit();        
        editor.putString(key, value); // value to store
        editor.commit();   

    }


    public void onClick(View v) {
        // TODO Auto-generated method stub

        savePrefs("NOTE", info.getText().toString());

        RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget);
        views.setTextViewText(R.id.tvConfigInput, info.getText());

        ComponentName thisWidget = new ComponentName(this, Widget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, views);

        Intent in = new Intent(c, WidgetConfig.class);
        PendingIntent pi = PendingIntent.getActivity(c, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);


        views.setOnClickPendingIntent(R.id.B_EditAgain, pi);

        awm.updateAppWidget(awID, views);


        Intent result = new Intent();
        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
        setResult(RESULT_OK, result);
        finish();

    }



}

UPDATE:

So i tried this, but nothings different, still doesn't work.

    private void loadPrefs(){
        sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
          , Context.MODE_PRIVATE);
        note = sp.getString("Note", "");

        info.setText(note);

    }

private void savePrefs(String key, String value){
    sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
              , Context.MODE_PRIVATE);
    Editor editor = sp.edit();
    editor.clear();
    editor.putString("Note", info.getText().toString());
    editor.putString(key, value); // value to store
    editor.commit();   

    }

解决方案

You probably don't want to use getDefaultSharedPreferences here. All widgets share the same default shared preferences, so they will be constantly overwriting each other.

I had the same situation in my own app, so I used a custom preference file for each widget. You can name the preference file with the widgetID, and then each widget will always get it's own unique set of preferences.

In the configuration PreferenceActivity:

this.getPreferenceManager().setSharedPreferencesName(
           "widget" + String.valueOf(mAppWidgetId)
);

This will store all of the PreferenceActivity settings into a preference file named after the widget id.

Then in the widget itself, just retrieve the file corresponding to its id:

preferences = context.getSharedPreferences(
      "widget" + String.valueOf(appWidgetId)
      , Context.MODE_PRIVATE);

And retrieve preferences like normal.

这篇关于添加几个appWidgets不同的配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆