[英]How to get text of a TextView of a Widget from an Activity?
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 the TextWidget of the activity. 我有一个小部件和一个活动,当我开始活动时,我必须从小部件的TextView中读取文本并将其显示在活动的TextWidget中。
My relevant 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
as null (I think I can't reference to a view object from another view.) Anyway, how could I do this? 调试它-我将
text_frase
为null(我认为我不能从另一个视图引用视图对象。)无论如何,我该怎么做?
No, you can't. 不,你不能。 I suggest one of this ways:
我建议采用以下一种方法:
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", "");
In your situation, I recommend method 1. 根据您的情况,我建议使用方法1。
you might want to get the actual String by using the .toString() method on the TextView. 您可能想通过在TextView上使用.toString()方法获取实际的String。
String frase;
TextView text_frase = (TextView) findViewById(R.id.widget_textview_frase);
if (text_frase.getText().toString() != null){
frase = (String) text_frase.getText().toString();
Log.v(LOG_CLASS_NAME, "frase: "+frase);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.