问题
When I use findViewById()
I get an error.
What method should I use, instead of findViewById()
in my app widget?
P.S.: I want to do something like:
Button a = (Button) findViewById(R.id.button);
in my screen widget
回答1:
You don't need a findViewById() in Widget just do this
create a static variable, which will be your onClick name tag:
private static final String MyOnClick = "Button OnClick";
Set this onClick tag to your view as below:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
views.setTextViewText(R.id.your_button, "Button Name");
views.setOnClickPendingIntent(R.id.your_button, MyOnClick);
get Button Click listener like bellow
public void onReceive(Context context, Intent intent) {
if (MyOnClick.equals(intent.getAction())){
//your onClick action is here
}
};
Here total code
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 Widget extends AppWidgetProvider {
private static final String SYNC_CLICKED = "automaticWidgetSyncButtonClick";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = new ComponentName(context, Widget.class);
remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED));
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if (SYNC_CLICKED.equals(intent.getAction())) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = new ComponentName(context, Widget.class);
remoteViews.setTextViewText(R.id.sync_button, "TESTING");
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
回答2:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#09C"
android:padding="@dimen/widget_margin">
<TextView
android:layout_width="300dp"
android:layout_height="match_parent"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:paddingTop="5dp"
android:text="New Text"
android:id="@+id/txt"
android:layout_centerVertical="true" />
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="^"
android:id="@+id/klik"
android:background="#09C"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
来源:https://stackoverflow.com/questions/44588098/what-method-i-must-use-instead-of-findviewbyid-in-app-widget