gpt4 book ai didi

java - 添加几个具有不同配置的 appWidgets?

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:11 25 4
gpt4 key购买 nike

我创建了一个显示简单 TextView 的小部件,它可以在配置 Activity 中作为 Edittext 进行编辑。我使用共享首选项保存输入的文本,因此用户可以点击小部件来编辑文本,并且已经输入的文本出现在编辑文本字段中。我的问题是这样的。我希望用户能够添加多个小部件,但是当添加第二个小部件时,将从共享首选项加载与另一个小部件中相同的文本。而且,当一个小部件被编辑时,另一个也是如此。希望我说清楚了。我有点知道它与 appWidgetIds 有关,但我无法弄清楚。

这是我的代码,有点简化。

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();

}



}

更新:

所以我尝试了这个,但没有什么不同,仍然不起作用。

    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();

}

最佳答案

您可能不想在这里使用 getDefaultSharedPreferences。所有小部件共享相同的默认共享首选项,因此它们将不断相互覆盖。

我在自己的应用程序中遇到了相同的情况,因此我为每个小部件使用了自定义首选项文件。您可以使用 widgetID 命名首选项文件,然后每个小部件将始终获得自己唯一的一组首选项。

在配置PreferenceActivity中:

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

这会将所有 PreferenceActivity 设置存储到以小部件 ID 命名的首选项文件中。

然后在小部件本身中,只需检索与其 id 对应的文件:

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

并像往常一样检索首选项。

关于java - 添加几个具有不同配置的 appWidgets?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12962524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com