【问题标题】:get text of a TextView in android home widget (not in activity)?在 android 主页小部件中获取 TextView 的文本(不在活动中)?
【发布时间】:2012-06-28 02:09:59
【问题描述】:

我刚开始使用 android home 小部件进行编程,所以我可以通过 settextviewtext() 从我的小部件提供程序将文本设置为 home 小部件中的 textview,但是当我想从中获取文本时,我没有理想。不能像在活动中一样正常使用 findViewById(),然后使用 getText()。有没有办法从小部件提供者那里获取主页小部件中的 textview 文本?或者告诉我如何从另一个活动中获得?

【问题讨论】:

  • 你应该养成在你提出的问题中标记正确答案的习惯;如果你这样做,人们会更倾向于给你好的答案。
  • 谢谢,我会的。这对我来说非常理想。

标签: android widget textview


【解决方案1】:

CommonsWare in this question 说:

你不能。应用程序小部件是只写的:您可以将数据推送给它们,但是 你无法阅读它们。

相反,当您使用新文本更新应用小部件时,您需要 将该文本存储在某个地方,也许是在一个文件中。

【讨论】:

    【解决方案2】:

    read this 获取小部件教程以及您可以用它做什么。

    【讨论】:

    • 是的,我已经阅读了该教程。但是我的问题肯定没有理想。但是我已经认识到我试图从 textview 获取文本有点愚蠢,因为是我自己为它设置了文本:))。谢谢大家。
    【解决方案3】:
    1. 在您实例化 App Widget 布局和显示的地方,请写下:

       SharedPreferences settings = 
       PreferenceManager.getDefaultSharedPreferences(context);
       @SuppressLint("CommitPrefEdits")
       SharedPreferences.Editor  editor = settings.edit();
       String rowid = "500"; 
       editor.putString("appWidgetRowId", rowid);
       editor.apply();
      
    2. 然后得到那个rowid(意思是500),在AppWidgetProvider中写这个:

      @Override
           public void onReceive(final Context context, Intent intent) {
               super.onReceive(context, intent);
      
       SharedPreferences settings = 
       PreferenceManager.getDefaultSharedPreferences(context);
                   @SuppressLint("CommitPrefEdits")
                   SharedPreferences.Editor  editor = settings.edit();
      
                   final String rowId= settings.getString("appWidgetRowId", null);
                   Toast toast = Toast.makeText(context, "Opening....  "+rowId, Toast.LENGTH_SHORT);
                   toast.setGravity(Gravity.CENTER|Gravity.BOTTOM, 0, 0);
                   toast.show();
       }
      

    你可以在Widget点击选项上调用第二种方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-02
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      • 1970-01-01
      • 2019-03-10
      • 2014-01-18
      相关资源
      最近更新 更多