java-在AppWidget上更新TextView文本大小

我无法弄清楚,我已经为它苦苦挣扎了好几天了,我对此感到非常厌倦.

我正在通过配置活动中的微调器更改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);

    }

}

strings.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));在窗口小部件提供程序中它本身.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...