繁体   English   中英

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

[英]How can I update my TextView on button click in widget?

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

主要活动:

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>

希望您已在AndroidMenifest.xml中注册了窗口小部件提供程序,如下所示:

<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>

可以从此处下载有效的源代码。

有关详细信息,请访问此博客

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM