对AppWidget更新的TextView文本大小

对AppWidget更新的TextView文本大小

本文介绍了对AppWidget更新的TextView文本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这一点,我一直在挣扎好几天了,我很厌倦了。

我是从一个配置活动变化上AppWidget有微调一个TextView的文本大小,我不能让它正确地更新。它不更新,我第一次告诉它,但它确实在第二和第三,每一次更新之后

我会真的真的AP preciate如果有人来看看我的code

非常感谢你。

配置类:

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

    对话myDialog;
    上下文的背景下= WidgetConfig.this;
    的EditText静态信息;
    静态微调微调;
    串appwidget_textsize =0;
    私有静态最后字符串[]路径= {10,12,14,16,18,20,
        22,24,26,28,30,32,34,36,38,40,50,60};

    私有静态最后弦乐preFS_NAME =com.harteg.NotesWidgetPro.Widget;
    私有静态最后弦乐preF_ preFIX_KEY =prefix_;

    私有静态最后字符串变量=MyActivity;

    INT mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    浮数;

    公共WidgetConfig(){
        超();
    }

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

        Log.v(TAG的onCreate()开始);

        的setContentView(R.layout.widgetconfig);
        上下文= WidgetConfig.this;
        //后退按钮=取消
        的setResult(RESULT_CANCELED);
        信息=(EditText上)findViewById(R.id.etwidgetconfig);

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

        //查找意图的小部件ID。
        意向意图= getIntent();
        捆绑额外= intent.getExtras();
        如果(临时演员!= NULL){
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        //如果他们给了我们一个意图不小部件ID,只是保释。
        如果(mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID){
            完();
        }
        info.setText(loadTitle preF(WidgetConfig.this,mAppWidgetId));

        // ------------文字大小微调---------------
        微调=(微调)findViewById(R.id.TxtSizeSP);


        ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(WidgetConfig.this,
                android.R.layout.simple_spinner_item,路径);

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

        spinner.setOnItemSelectedListener(本);

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

    } //的onCreate完成

    公共无效onItemSelected(适配器视图<>母公司,视图V,INT位置,长的id){
        Log.v(TAG,OnItemselected开始);

        开关(位置){
        情况下0:
            info.setTextSize(10.0f);
            Log.v(TAG,chosed位置0);
            appwidget_textsize =10的ToString();
            打破;
        ...

         }
         数= Float.valueOf(appwidget_textsize);
        }


    公共无效onNothingSelected(适配器视图<>为arg0){

    }

    View.OnClickListener mOnClickListener =新View.OnClickListener(){
        公共无效的onClick(视图v){

            Log.v(TAG,设置远程视图);
            RemoteViews意见=新RemoteViews(context.getPackageName(),R.layout.widget);
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(上下文);

            Log.v(TAG,设置TXT大小);
            views.setFloat(R.id.tvConfigInputsetTextSize,号码);
            appWidgetManager.updateAppWidget(mAppWidgetId,意见);
            Log.v(TAG,过TXT大小);

            //当单击该按钮,保存在字符串中我们preFS和
            //返回他们点击确定。
            。字符串标题preFIX = info.getText()的toString();
            saveTitle preF(背景下,mAppWidgetId,标题preFIX);


            //推小窗口更新与新设置的preFIX浮出水面
            Widget.updateAppWidget(背景下,appWidgetManager,mAppWidgetId,
                    标题preFIX,意见);

            //确保我们传回了原来的appWidgetId
            意图resultValue =新意图();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    mAppWidgetId);
            的setResult(RESULT_OK,resultValue);
            Log.v(TAG的onClick做);
            完();

        }
    };

    //写preFIX到共享preferences对象为这个小工具
    静态无效saveTitle preF(上下文的背景下,INT mAppWidgetId,文本字符串){
        共享preferences.Editor编辑= context.getShared preferences(preFS_NAME,0).edit();

        editor.putString(preF_ preFIX_KEY + mAppWidgetId,文本);

        //保存值
        editor.commit();
    }

    //读取从共享preferences的preFIX对象为这个小程序。
    静态字符串loadTitle preF(上下文的背景下,INT mAppWidgetId){
        共享preferences preFS = context.getShared preferences(preFS_NAME,MODE_PRIVATE);

        字符串preFIX = prefs.getString(preF_ preFIX_KEY + mAppWidgetId,NULL);

        //如果没有preference保存,得到从资源的默认
        如果(preFIX!= NULL){
            返回preFIX;
        } 其他 {
            返回context.getString(R.string.appwidget_ prefix_default);
        }
    }

    静态无效deleteTitle preF(上下文的背景下,INT mAppWidgetId){
    }

    静态无效loadAllTitle preFS(上下文的背景下,
            ArrayList的<整数GT; mAppWidgetIds,ArrayList的<字符串>文本){
    }

}
 

AppWidgetProvider类:

 公共类的Widget扩展AppWidgetProvider {

    私有静态最后字符串变量=MyActivity;


    @覆盖
    公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
            INT [] appWidgetIds){

        Log.v(TAG的onupdate开始);
        // TODO自动生成方法存根
        //super.onUpdate(context,appWidgetManager,appWidgetIds);

        最终诠释N = appWidgetIds.length;
        RemoteViews意见=新RemoteViews(context.getPackageName(),R.layout.widget);

        的for(int i = 0; I&n种;我++){
            INT mAppWidgetId = appWidgetIds [I]
            字符串标题preFIX = WidgetConfig.loadTitle preF(背景下,mAppWidgetId);
                    updateAppWidget(背景下,appWidgetManager,mAppWidgetId,标题preFIX,意见);

        }
    }

    @覆盖
    公共无效onDeleted(上下文的背景下,INT [] appWidgetIds){

        最终诠释N = appWidgetIds.length;
        的for(int i = 0; I&n种;我++){
            WidgetConfig.deleteTitle preF(背景下,appWidgetIds [I]);
        }
    }

    ...


    静态无效updateAppWidget(上下文的背景下,AppWidgetManager appWidgetManager,
            INT mAppWidgetId,字符串标题preFIX,RemoteViews意见){
        Log.v(TAG,updateAppWidget开始);
        //获取字符串这种方式允许本地化的字符串。该格式
        //字符串填充使用的java.util.Formatter风格的格式字符串。
        CharSequence的文字= context.getString(R.string.appwidget_text_format,
              WidgetConfig.loadTitle preF(背景下,mAppWidgetId));

        appWidgetManager = AppWidgetManager.getInstance(上下文);

        ...

        意见=新RemoteViews(context.getPackageName(),R.layout.widget);
        views.setTextViewText(R.id.tvConfigInput,文本);

        //告诉AppWidgetManager对当前应用程序部件进行更新
        Log.v(TAG,底部更新开始);
        appWidgetManager.updateAppWidget(mAppWidgetId,意见);

    }

}
 

的strings.xml

 <资源的xmlns:XLIFF =金塔:绿洲:名称:TC:XLIFF:文件:1.2>
    <字符串名称=appwidget_ prefix_default>< /串>
    <字符串名称=appwidget_textsize>< /串>
    <字符串名称=appwidget_text_format>< XLIFF:摹ID =preFIX>%1 $ S< / XLIFF:G>< /串>
    ...
< /资源>
 

解决方案

所以,我终于得到它想要一百万个不同的事情后工作。最后我传递一个字符串,通过共享preferences,以同样的方式我得到了文本的小部件,以及刚刚成立 views.setFloat(R.id.TextView,setTextSize的价值, Float.valueOf(SizeString)); 窗口小部件提供它的自我。

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

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

Thank you very much

Config class:

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

    }

}

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>
解决方案

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.

这篇关于对AppWidget更新的TextView文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:20