为app小部件实现onclick监听器

时间:2012-11-20 12:13:30

标签: android android-widget

我有一个应用程序小部件,显示一些rss feeds.It包含一个文本视图,它将显示新闻标题和图像视图到diplay图像。在该小部件中,我想添加两个按钮,其功能为下一个和上一个导航通过新闻标题。我知道如何在活动中执行此操作,但如何在窗口小部件中为这些按钮实现onclick监听器

这是我的xml =>

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
<ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:scaleType="centerCrop"
            android:src="@drawable/logo" />
<ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/imageView1"
            android:scaleType="centerCrop"
            android:src="@drawable/titlebg2" />
<ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:layout_below="@+id/imageView2"
            android:layout_centerHorizontal="true"
            android:scaleType="centerCrop"
            android:src="@drawable/foot" />
<ImageView
            android:id="@+id/imageView4"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_alignParentLeft="true"
            android:layout_alignTop="@+id/imageView2"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:scaleType="centerCrop"
            android:src="@drawable/img" />
<Button
            android:id="@+id/next"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/imageView1"
            android:layout_marginRight="3dp"
            android:layout_marginTop="3dp"
            android:background="@drawable/up" />


        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/up"
            android:layout_alignBottom="@+id/up"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="25dp"
            android:layout_toRightOf="@+id/imageView4"
            android:text="TextView"
            android:textColor="#ffff" />
      <Button
            android:id="@+id/previous"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignBottom="@+id/imageView2"
            android:layout_alignLeft="@+id/up"
            android:layout_marginBottom="3dp"
            android:background="@drawable/down" />

    </RelativeLayout>

</LinearLayout>

Follwing是我的onupdate函数, =&GT;

 public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
        {

         parse();//Here Iam parsing the xml
         RemoteViews remoteViews;
             ComponentName watchWidget;


            remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget_main );
            watchWidget = new ComponentName( context, madhyamambig.class );

            remoteViews.setTextViewText( R.id.title, Title[0]);


            Bitmap bitmap;
            try {
                bitmap = BitmapFactory.decodeStream((InputStream)new URL(image[0]).getContent());


                 remoteViews.setImageViewBitmap(R.id.imageView4, bitmap);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                            appWidgetManager.updateAppWidget( watchWidget, remoteViews );

        }

在一项活动中,我们可以简单地这样做,

     int i=0;
      next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {


 Bitmap bitmap;
                try {
                    bitmap = BitmapFactory.decodeStream((InputStream)new      URL(image[i+1]).getContent());


                     remoteViews.setImageViewBitmap(R.id.imageView4, bitmap);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                                appWidgetManager.updateAppWidget( watchWidget, remoteViews );

            }
          }


        });

我想在widget中实现相同的功能。有人请帮助我......提前致谢..

1 个答案:

答案 0 :(得分:2)

您可以使用RemoteViews.setOnClickPendingIntent方法。

然后启动将由服务,广播接收器或服务捕获的PendingIntent。此意图所针对的上下文将能够再次使用RemoteView来操作源窗口小部件。

点击处理有点复杂,因为这里的所有内容都是在远程过程中操纵UI元素。

相关问题