如何在窗口小部件中单击按钮更新我的TextView?

时间:2015-03-21 13:47:52

标签: android widget click

大家好我正在使用报价应用程序,我已经创建了一个引号的小部件,当用户按下按钮时,我还在引号旁边放了一个按钮; textview应该改变,我几乎放弃了这个该死的小部件!通过互联网研究了1000次,但没有找到解决方案!任何帮助将不胜感激!我想在id更改的按钮的帮助下更改id为q1的TextView

MainActivity:

package com.example.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MainActivity extends AppWidgetProvider {

    public static String CHANGE_BUTTON = "com.example.widget.CHANGE_BUTTON";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        for (int i = 0; i < appWidgetIds.length; i++) {
            int currentWidgetId = appWidgetIds[i];
            String url = "http://www.tutorialspoint.com";
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse(url));

            RemoteViews views2 = new RemoteViews(context.getPackageName(),
                    R.layout.activity_main);

            Intent intent2 = new Intent(CHANGE_BUTTON);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                    0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
            views2.setOnClickPendingIntent(R.id.changes, pendingIntent);

            appWidgetManager.updateAppWidget(currentWidgetId, views2);
            Toast.makeText(context, "Widget Added Successfully!",
                    Toast.LENGTH_SHORT).show();
        }
    }

}

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="top"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <TextView
      android:id="@+id/q1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="20sp"
      android:padding="6dp"
      android:gravity="center"
      android:layout_alignParentTop="true"
      android:text="@string/q1"
      android:background="@drawable/stroke"/>

   <Button
       android:id="@+id/changes"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/q1"
       android:layout_centerHorizontal="true"
       android:text="Change"
       android:layout_marginTop="1dp" />

</RelativeLayout>

widget.xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/activity_main"
    android:minHeight="110dp"
    android:minWidth="250dp"
    android:updatePeriodMillis="0" >


</appwidget-provider>

1 个答案:

答案 0 :(得分:0)

希望您已在AndroidMenifest.xml中注册了widget提供程序,如下所示:

<receiver android:name="MyWidgetProvider" >
            <intent-filter >
                <action 
                    android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/demo_widget_provider" />
        </receiver>

你必须创建这样的广播意图接收器:

public class MyWidgetIntentReceiver extends BroadcastReceiver {

    private static int clickCount = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_PICTURE")){
            updateWidgetPictureAndButtonListener(context);
        }
    }

    private void updateWidgetPictureAndButtonListener(Context context) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_demo);
        remoteViews.setImageViewResource(R.id.widget_image, getImageToSet());

        //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
        remoteViews.setOnClickPendingIntent(R.id.widget_button, MyWidgetProvider.buildButtonPendingIntent(context));

        MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
    }

    private int getImageToSet() {
        clickCount++;
        return clickCount % 2 == 0 ? R.drawable.me : R.drawable.wordpress_icon;
    }
}

你还必须注册广播接收器:

<receiver
    android:name="MyWidgetIntentReceiver"
    android:label="widgetBroadcastReceiver" >
    <intent-filter>
        <action android:name="pl.looksok.intent.action.CHANGE_PICTURE" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/demo_widget_provider" />
</receiver>

可以下载工作源代码from here

有关详细信息,请查看this blog