How to get text of a TextView of a Widget from an Activity?

前端 未结 2 623
说谎
说谎 2021-01-15 00:41

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.

My rel

相关标签:
2条回答
  • 2021-01-15 01:14

    No, you can't. I suggest one of this ways:

    1. 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", "");
    
    1. Using SharedPreferences for saving your text and loading it in the activity.

    In your situation, I recommend method 1.

    0 讨论(0)
  • 2021-01-15 01:34

    you might want to get the actual String by using the .toString() method on the TextView.

    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);
    }
    
    0 讨论(0)
提交回复
热议问题