应用小部件配置活动 - 如何访问我的小部件视图?

时间:2018-03-13 09:40:15

标签: android widget android-widget android-appwidget appwidgetprovider

我创建了一个窗口小部件配置文件,该文件在我的应用程序窗口小部件放置在屏幕上时启动。但是,我不知道如何从我的小部件布局访问按钮以对其进行更改(在此示例中,我想更改颜色/色调)

public class ExampleAppWidgetConfig extends AppCompatActivity {
private int appWidgetId;
private RadioGroup radioGroupColor;
private RadioButton radioButtonDefault;
private RadioButton radioButtonGreen;
private RadioButton radioButtonRed;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_example_app_widget_config);

    Intent configIntent = getIntent();
    Bundle extras = configIntent.getExtras();
    if (extras != null) {
        appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    radioGroupColor = findViewById(R.id.radio_group_color);
    radioButtonDefault = findViewById(R.id.radio_button_default);
    radioButtonGreen = findViewById(R.id.radio_button_green);
    radioButtonRed = findViewById(R.id.radio_button_red);
}

public void confirmConfiguration(View v) {


    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);

    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.example_widget);
    views.setOnClickPendingIntent(R.id.example_widget_button, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, views);

    Intent resultValue = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    setResult(RESULT_OK, resultValue);
    finish();
}

}

0 个答案:

没有答案