单击按钮后,如何更改文本视图的文本?
我试图设置一个悬疑的词,该方法唤起了“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
意图是Android从一项活动变为另一种活动的本地方式。这些不会更改目标/起点类中的任何内容。没有直接的方法可以使用意图将上下文传递给目标活动。同样,您也无法从意图中调用特定方法。但是有一个解决方法,您可以做这样的事情:
然后在所谓的活动中,您可以采取意图并决定这样称呼的方法:
希望它可以帮助您
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 :
and then in the called activity , you can take the intent and decide which method to call like this:
Hope it helps