我有一个显示一些 rss 提要的应用程序小部件。它包含一个显示新闻标题的文本视图和一个显示图像的图像视图。在该小部件中,我想添加两个按钮,它们的作用是下一个和上一个,以浏览新闻标题。我知道如何在活动中执行此操作,但是如何在小部件中为这些按钮实现 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>
以下是我的 onupdate 函数,=>
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 );
}
}
});
我想在小部件中实现相同的功能。有人请帮助我...在此先感谢..