1

私は、ウィジェット上にあるボタンを押すだけでウィジェットが更新されるようにするために、執拗に努力してきました。ウィジェットには、現在のRAMの残量、CPUレベル、およびバッテリーの充電量が表示されます。

これらの値を決定するコードはうまく機能します。ボタンを押しても更新できません。私はこれらの方法を試しました:

Androidウィジェットを強制的に更新します-両方の方法が示されています

Android:特定の種類のすべてのウィジェットを強制的に更新するにはどうすればよいですか?

Androidのクリック可能なウィジェット

そして警報マネージャー

それらのどれも働いていません。

ウィジェットレイアウトのXML:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:background="@drawable/widgetbg">

        <TextView android:id="@+id/widgetCPU"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/derp"   
            android:textSize="14sp"
            android:typeface="sans"
            android:textColor="@android:color/holo_blue_bright"
            android:gravity="center"/>

        <TextView android:id="@+id/widgetRam"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/derp"   
            android:textSize="14sp"
            android:typeface="sans"
            android:textColor="@android:color/holo_blue_bright"
            android:gravity="center"/>

        <TextView android:id="@+id/widgetBat"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/derp"   
            android:textSize="14sp"
            android:typeface="sans"
            android:textColor="@android:color/holo_blue_bright"
            android:gravity="center"/>
        <Button android:id="@+id/widgetUpdate"
                android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/button_send" />

    </LinearLayout>

そして、ボタンが押されたときに呼び出したいメソッド:

    public class AppWidget extends AppwidgetProvider
    {
    //...
         public void update(Context c, AppWidgetManager a)
         { 
          //...
         }
    //...
    }

ボタンが押されたときにupdateメソッドを呼び出すにはどうすればよいですか?

4

2 に答える 2

1

onClickListenerをボタンに追加し、リスナー内からupdateメソッドを呼び出すことができます。

インターフェイスの詳細については、このリンクを確認してください

于 2013-01-12T22:26:54.563 に答える
1

たとえば、onCreate()メソッドでリスナーを登録します。

     findViewById(R.id.widgetUpdate).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                        update();
        }
    });
于 2013-01-12T22:31:57.710 に答える