如何获得一个Widget从一个活动一个TextView的文字? [英] How to get text of a TextView of a Widget from an Activity?
本文介绍了如何获得一个Widget从一个活动一个TextView的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个小工具......和活动......当我开始活动......我已经从部件的TextView的阅读文本并显示在活动...的TextWidget
我有code ...
字符串frase =;
TextView的text_frase =(TextView中)findViewById(R.id.widget_textview_frase);
如果(text_frase!= NULL){
frase =(字符串)text_frase.getText();
}
Log.v(LOG_CLASS_NAMEfrase:+ frase);
调试它,我有 text_frase 空(我想我不能引用者从另一种观点认为视图对象......反正...我该怎么办?
解决方案
没有,你不能。我建议这个方法之一:
- 添加文本到一个包,并获取它在你的活动。
在你的小工具:
意向意图=新的意图(背景下,YourActivity.class);
叠B =新包();
b.putString(文本,yourtext);
intent.putExtras(B);
PendingIntent pendingIntent = PendingIntent.getActivity(背景下,appWidgetId,意向,PendingIntent.FLAG_CANCEL_CURRENT);
在您的活动:
意向意图= getIntent();
叠B = intent.getExtras();
字符串文本= b.getString(文字,);
- 在使用共享preferences为节省您的文本,并加载了活动。
在你的情况,我建议的方法1。
i have a widget... and an activity... when i start the activity... i have to read text from the textView of the widget and show it in a TextWidget of the activity...
i have that code...
String frase="";
TextView text_frase = (TextView) findViewById(R.id.widget_textview_frase);
if (text_frase != null){
frase = (String) text_frase.getText();
}
Log.v(LOG_CLASS_NAME, "frase: "+frase);
debugging it i have text_frase null (i think i can't referer to a view object from another view... anyway... how can i do?
解决方案
No, you can't. I suggest one of this ways:
- Adding your text to a Bundle and fetching it in your activity.
in your widget:
Intent intent = new Intent(context, YourActivity.class);
Bundle b= new Bundle();
b.putString("Text", yourtext);
intent.putExtras(b);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
in your activity:
Intent intent=getIntent();
Bundle b= intent.getExtras();
String text= b.getString("Text", "");
- Using SharedPreferences for saving your text and loading it in the activity.
In your situation, i recommend method 1.
这篇关于如何获得一个Widget从一个活动一个TextView的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文