单击按钮后,如何更改文本视图的文本?

发布于 2025-02-13 01:50:39 字数 1939 浏览 0 评论 0原文

我试图设置一个悬疑的词,该方法唤起了“ shinkthenumber()”的方法,“ shinknumber.java”,然后单击按钮。

问题在于,在设想该方法时,我会收到以下错误:

无法解析构造函数的意图(android.content.context,void)

com。 example.myapplication.shrinknumber

public static void shrinkthenumber(@nonnull上下文上下文)

我无法弄清楚问题是什么。

整个程序的目标是单击按钮时文本视图的文本会更改。

class缩水:


import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import android.widget.RemoteViews;
public class shrinkNumber {

    public static void shrinkTheNumber(Context context)

    {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        remoteViews.setTextViewText(R.id.textView, "hello");
        updatewidgetnow(context, remoteViews);
    }

    public static void updatewidgetnow (Context context, RemoteViews remoteViews){
        ComponentName widgetComponent = new ComponentName(context, WidgetProvider.class);
        AppWidgetManager.getInstance(context).updateAppWidget(widgetComponent, remoteViews);


    }
}

这是垂悬的部分:

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        Intent shrinkIntent = new Intent(context, shrinkNumber.shrinkTheNumber(context));

        PendingIntent shrinkPendingIntent =  PendingIntent.getActivity(context,0,shrinkIntent,0);

        remoteViews.setOnClickPendingIntent(R.id.button2, shrinkPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);


        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

I tried to setup a pendingIntent which evokes the method "shrinkTheNumber()" of the class "shrinkNumber.java" after the button is clicked.

The Problem is that at the point of envoking that method i get the following error:

Cannot resolve constructor 'Intent(android.content.Context, void)

com.example.myapplication.shrinkNumber

public static void shrinkTheNumber(@NonNull Context context)

I can't figure out what the problem is.

The goal of the whole program is that the text of the textView gets changed when the button is clicked.

class shrinkNumber:


import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import android.widget.RemoteViews;
public class shrinkNumber {

    public static void shrinkTheNumber(Context context)

    {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        remoteViews.setTextViewText(R.id.textView, "hello");
        updatewidgetnow(context, remoteViews);
    }

    public static void updatewidgetnow (Context context, RemoteViews remoteViews){
        ComponentName widgetComponent = new ComponentName(context, WidgetProvider.class);
        AppWidgetManager.getInstance(context).updateAppWidget(widgetComponent, remoteViews);


    }
}

Here is the part with the pendingIntent:

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        Intent shrinkIntent = new Intent(context, shrinkNumber.shrinkTheNumber(context));

        PendingIntent shrinkPendingIntent =  PendingIntent.getActivity(context,0,shrinkIntent,0);

        remoteViews.setOnClickPendingIntent(R.id.button2, shrinkPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);


        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2025-02-20 01:50:39

意图是Android从一项活动变为另一种活动的本地方式。这些不会更改目标/起点类中的任何内容。没有直接的方法可以使用意图将上下文传递给目标活动。同样,您也无法从意图中调用特定方法。但是有一个解决方法,您可以做这样的事情:

Intent intent = new Intent(this, shrinkNumber.class);
intent.putExtra("methodName","shrinkTheNumber");

然后在所谓的活动中,您可以采取意图并决定这样称呼的方法:

if(intent.getStringExtra("methodName").equals("shrinkTheNumber")){
      shrinkTheNumber();
   }

希望它可以帮助您

Intent is Android's native way to change from one activity to another. These do not change anything in the target/starting point class. There is no direct way to pass the context to the target activity using Intent. Similarly you can't call a particular method from intent. But there is a workaround for this, you can do something like this :

Intent intent = new Intent(this, shrinkNumber.class);
intent.putExtra("methodName","shrinkTheNumber");

and then in the called activity , you can take the intent and decide which method to call like this:

if(intent.getStringExtra("methodName").equals("shrinkTheNumber")){
      shrinkTheNumber();
   }

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文