更新 AppWidget 上的 TextView 文本大小

2022-01-18 00:00:00 widget android java android-widget

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

I can not figure this out, I've been struggling with it for days now, and I'm so tired of it..

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

I'm changing the text size of a TextView on an AppWidget with a Spinner from a Configuration Activity, and I can't get it to update correctly. It does not update the first time I tell it to, but it does update the second and third and every time afterwards.

如果有人能看一下我的代码,我将不胜感激

I'll really really appreciate if someone would take a look at my code

非常感谢

配置类:

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 类:

AppWidgetProvider class:

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>

推荐答案

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

So i finally got it working after trying a million different things. I ended up passing a string with the value via sharedpreferences, the same way i got the text to the widget, and just set views.setFloat(R.id.TextView, "setTextSize", Float.valueOf(SizeString)); in the widget provider it self.

相关文章