1

我想不通,我已经为此苦苦挣扎了好几天了,我已经厌倦了..

我正在使用配置活动中的微调器更改 AppWidget 上 TextView 的文本大小,但我无法正确更新它。我第一次告诉它时它不会更新,但它确实会更新第二次和第三次以及之后的每次。

如果有人能看一下我的代码,我会非常感激

非常感谢你

配置类:

public class WidgetConfig extends Activity implements OnItemSelectedListener{

    Dialog myDialog;
    Context context = WidgetConfig.this;
    static EditText info;
    static Spinner spinner;
    String appwidget_textsize = "0";
    private static final String[] paths = { "10", "12", "14", "16", "18", "20",
        "22", "24", "26", "28", "30", "32", "34", "36", "38", "40", "50", "60"};

    private static final String PREFS_NAME = "com.harteg.NotesWidgetPro.Widget";
    private static final String PREF_PREFIX_KEY = "prefix_";

    private static final String TAG = "MyActivity";

    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;   

    float number;

    public WidgetConfig() {
        super();
    }

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

        Log.v(TAG, "onCreate() started");

        setContentView(R.layout.widgetconfig);
        context = WidgetConfig.this;
        // back button = cancel
        setResult(RESULT_CANCELED);
        info = (EditText) findViewById(R.id.etwidgetconfig);

        findViewById(R.id.bwidgetconfig).setOnClickListener(mOnClickListener);
        findViewById(R.id.bwidgetconfig1).setOnClickListener(mOnClickListener);

        // 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 they gave us an intent without the widget id, just bail.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }
        info.setText(loadTitlePref(WidgetConfig.this, mAppWidgetId));

        //------------ Text Size spinner ---------------
        spinner = (Spinner) findViewById(R.id.TxtSizeSP);


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(WidgetConfig.this,
                android.R.layout.simple_spinner_item, paths);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(this); 

        //--------------------------------------------------

    } // onCreate finished

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        Log.v(TAG, "OnItemselected started");

        switch (position) {
        case 0:
            info.setTextSize(10.0f);
            Log.v(TAG, "position 0 chosed");
            appwidget_textsize = "10".toString();
            break;
        ... 

         }
         number = Float.valueOf(appwidget_textsize);    
        }


    public void onNothingSelected(AdapterView<?> arg0) {

    }

    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {

            Log.v(TAG, "set remote view");
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            Log.v(TAG, "set txt size");
            views.setFloat(R.id.tvConfigInput, "setTextSize", number);
            appWidgetManager.updateAppWidget(mAppWidgetId, views);
            Log.v(TAG, "over txt size");

            // When the button is clicked, save the string in our prefs and
            // return that they clicked OK.
            String titlePrefix = info.getText().toString();
            saveTitlePref(context, mAppWidgetId, titlePrefix);


            // Push widget update to surface with newly set prefix
            Widget.updateAppWidget(context, appWidgetManager, mAppWidgetId,
                    titlePrefix, views);

            // Make sure we pass back the original appWidgetId
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    mAppWidgetId);
            setResult(RESULT_OK, resultValue);
            Log.v(TAG, "onClick done");
            finish();

        }
    };

    // Write the prefix to the SharedPreferences object for this widget
    static void saveTitlePref(Context context, int mAppWidgetId, String text) {
        SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();

        editor.putString(PREF_PREFIX_KEY + mAppWidgetId, text);

        // Saves the values
        editor.commit();
    }

    // Read the prefix from the SharedPreferences object for this widget.
    static String loadTitlePref(Context context, int mAppWidgetId) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

        String prefix = prefs.getString(PREF_PREFIX_KEY + mAppWidgetId, null);

        // If there is no preference saved, get the default from a resource
        if (prefix != null) {
            return prefix;
        } else {
            return context.getString(R.string.appwidget_prefix_default);
        }
    }

    static void deleteTitlePref(Context context, int mAppWidgetId) {
    }

    static void loadAllTitlePrefs(Context context,
            ArrayList<Integer> mAppWidgetIds, ArrayList<String> texts) {
    }

}

AppWidgetProvider 类:

public class Widget extends AppWidgetProvider {

    private static final String TAG = "MyActivity";


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        Log.v(TAG, "onUpdate started");
        // TODO Auto-generated method stub
        //super.onUpdate(context, appWidgetManager, appWidgetIds);

        final int N = appWidgetIds.length;
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

        for (int i=0; i<N; i++){
            int mAppWidgetId = appWidgetIds[i];
            String titlePrefix = WidgetConfig.loadTitlePref(context, mAppWidgetId);
                    updateAppWidget(context, appWidgetManager, mAppWidgetId, titlePrefix, views);

        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {

        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
            WidgetConfig.deleteTitlePref(context, appWidgetIds[i]);
        }
    }

    ...


    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
            int mAppWidgetId, String titlePrefix, RemoteViews views) {
        Log.v(TAG, "updateAppWidget started");
        // Getting the string this way allows the string to be localized.  The format
        // string is filled in using java.util.Formatter-style format strings.
        CharSequence text = context.getString(R.string.appwidget_text_format,
              WidgetConfig.loadTitlePref(context, mAppWidgetId));        

        appWidgetManager = AppWidgetManager.getInstance(context); 

        ...

        views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setTextViewText(R.id.tvConfigInput, text);

        // Tell the AppWidgetManager to perform an update on the current app widget
        Log.v(TAG, "Bottom update started");
        appWidgetManager.updateAppWidget(mAppWidgetId, views);

    }

}

字符串.xml

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <string name="appwidget_prefix_default"></string>
    <string name="appwidget_textsize"></string>
    <string name="appwidget_text_format"><xliff:g id="prefix">%1$s</xliff:g></string> 
    ...   
</resources>
4

2 回答 2

1

所以我在尝试了一百万种不同的东西后终于让它工作了。我最终通过 sharedpreferences 传递了一个带有值的字符串,就像我将文本获取到小部件一样,并且只是views.setFloat(R.id.TextView, "setTextSize", Float.valueOf(SizeString));在小部件提供程序中自行设置。

于 2012-11-25T10:16:34.417 回答
0

您必须在 ACTION_APPWIDGET_UPDATE 启动配置 Activity 时首次广播自己。

Appwidgets文档说:

创建 App Widget 时不会调用 onUpdate() 方法(启动配置 Activity 时系统不会发送 ACTION_APPWIDGET_UPDATE 广播)。首次创建 App Widget 时,配置 Activity 负责向 AppWidgetManager 请求更新。然而,onUpdate() 将被调用以进行后续更新——它只是在第一次被跳过。

更新:

尝试将此代码添加到 Config 活动的 finish() 行上方。

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetManager.updateAppWidget(
        new ComponentName(this.getPackageName(), Widget.class.getName()), views);
于 2012-11-24T03:34:51.633 回答