我正在开发一个 android 小部件。在小部件中,我有一个文本字段和两个按钮。当我按下 button1 时,我希望文本字段显示某个文本,例如“one”,如果单击 button2,文本字段将显示为“2”。
onUpdate() 看起来像这样:
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){ super.onUpdate(context, appWidgetManager, appWidgetIds);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
Intent intent = new Intent(context, CalculatorReceiver.class);
intent.setAction(ACTION_WIDGET_RECEIVER);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.one, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); }
onReceive() 方法如下所示:
@Override public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getAction().equals(ACTION_WIDGET_RECEIVER)){
Toast.makeText(context, "Hi I'm 1", Toast.LENGTH_LONG).show();
}}
现在,在小部件中,我只能在按下按钮时显示 Toast,但我想获取 textView 并更改其值。如何获取和更改 textView?