Android 带有按钮并通过服务更新的主屏幕小部件的示例代码

Android 带有按钮并通过服务更新的主屏幕小部件的示例代码,android,android-widget,Android,Android Widget,我已经阅读了许多关于这个主题的教程,但无法使其发挥作用。有人能帮我做以下事情吗 a) 具有按钮、Imageview和TextView的主屏幕小部件 b) textview通过服务定期更新 c) 单击按钮后,图像将更改 任何人都可以提供示例代码或指向实现此功能的代码吗?看看这本书,它非常清楚地解释了如何使小部件“静音模式切换” a)在xml中,以下是代码 <Button android:id="@+id/b" android:layout_width="wrap_conten

我已经阅读了许多关于这个主题的教程,但无法使其发挥作用。有人能帮我做以下事情吗

a) 具有按钮、Imageview和TextView的主屏幕小部件 b) textview通过服务定期更新 c) 单击按钮后,图像将更改


任何人都可以提供示例代码或指向实现此功能的代码吗?

看看这本书,它非常清楚地解释了如何使小部件“静音模式切换”

a)在xml中,以下是代码

<Button
    android:id="@+id/b"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/go" />
<ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/image" />
<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@string/welcome" />
要定期执行此操作,可以使用并行线程概念,例如runnable

c) 为此目的设置一个点击式侦听器

Button b1;
b1 = (Button) findViewById(R.id.b);
b1.setOnClickListener(button_func);
这在onCreate方法中实现

View.OnClickListener button_func = new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
               image = (ImageView) findViewById(R.id.iv);
               iv.setImageResource(R.id.new_image);

                                            }
                  };

希望您觉得这很有用

Hi Arun,谢谢,但这是一个普通的小部件,主屏幕小部件与findViewbyId不起作用。相应的语法更改可在
View.OnClickListener button_func = new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
               image = (ImageView) findViewById(R.id.iv);
               iv.setImageResource(R.id.new_image);

                                            }
                  };