how can get text of textview that placed on a widget in android programming

最后都变了- 提交于 2019-12-12 11:10:43

问题


i have a question. i did all things for design and coding a widget before. now i have a button and a textview in my widget. i want when i click on my button my text entered in textview puted as extra for an intent and then start that intent. i can do this with following code in :

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        for (int i=0;i<appWidgetIds.length;i++){
            int awID=appWidgetIds[i];
            RemoteViews v=new RemoteViews(context.getPackageName(),R.layout.widgetshow);
.
.
.

            Intent in = new Intent("my.app.NOTEEDITOR");
            Bundle basket = new Bundle();
        */100*/ basket.putString("textViewText", "test");
            in.putExtras(basket);
            PendingIntent pi = PendingIntent.getActivity(context, 0, in, 0);
            v.setOnClickPendingIntent(R.id.button, pi);

            appWidgetManager.updateAppWidget(awID, v);
.
.
.
        }
    }

but in line marked /100/ i want put my textviews' text as extra. i want when i click on my button in the widget that start a activity with an extra contained text of textview placed on widget that i clicked on it's button.

sorry guys for my terrible english speaking


回答1:


You don't need to get the TextView's text in your widget. why? because the TextView's text will change in another place and then this changes will apply to widget. For example, the user can change the text in a activity. For showing the new text in widget, you can save it in a SharedPreferences and fetch it in your widget.

Refer to here for more details.




回答2:


I have the same problem with you. And someone like aTa don't know what is my expected things exactly. And i suggest you that you can use sharedPreference to save data of TextView that placed on you android home widget, then your activity get data from it. This is a cheat




回答3:


update your code:

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        for (int i=0;i<appWidgetIds.length;i++){
            int awID=appWidgetIds[i];
            RemoteViews v=new RemoteViews(context.getPackageName(),R.layout.widgetshow);


            Intent in = new Intent(paramContext, Caller2.class); ///Activity name Caller2.class  you want to start
            Bundle basket = new Bundle();
            TextView txt=(TextView)findViewbyId(R.id.txtid);
            String test = tx.getText().toString();
            basket.putString("textViewText", test);
            in.putExtras(basket);
            PendingIntent pi = PendingIntent.getActivity(context, 0, in, 0);
            v.setOnClickPendingIntent(R.id.button, pi);
            appWidgetManager.updateAppWidget(awID, v);

        }
    }

for more help see Widget is clicked



来源:https://stackoverflow.com/questions/9748161/how-can-get-text-of-textview-that-placed-on-a-widget-in-android-programming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!