Android小部件配置活动

时间:2017-09-26 10:04:26

标签: android xamarin widget

美好的一天!我有一个小部件,我需要使用简单的微调器和确认按钮构建配置活动。

我已将其添加到应用小部件提供程序中,当我将小部件添加到屏幕时,正在调用该活动。但它在活动启动后立即崩溃。我看不到配置布局。

[Activity(Label = "TestConfig", Name = "com.test.ConfigureActivityBig")]
    [IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_CONFIGURE" })]
    class ConfigureActivity: Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetResult(Result.Canceled);
            SetContentView(Resource.Layout.WidgetConfig);

            var spinner = (Spinner) FindViewById(Resource.Id.widgetSpinner);
            var okButton = FindViewById(Resource.Id.widgetConfigButton);

            var accountList = new List<string> {"1", "2"};
            ArrayAdapter adapter = new ArrayAdapter(this, Resource.Layout.WidgetConfig, accountList);
            spinner.Adapter = adapter;

            var widgetManager = AppWidgetManager.GetInstance(this);
            var configView = new RemoteViews(PackageName, Resource.Layout.MainWidgetLayout);

            var widgetId =Intent.GetIntExtra(AppWidgetManager.ExtraAppwidgetId, AppWidgetManager.InvalidAppwidgetId);
            if (widgetId == AppWidgetManager.InvalidAppwidgetId)
            {
                Finish();
                return;
            }

            okButton.Click += delegate
            {
                var selectedItem = spinner.SelectedItemPosition == 0 ? "1" : "2";

                configView.SetTextViewText(Resource.Id.widgetNumberLabel, selectedItem);
                widgetManager.UpdateAppWidget(widgetId,configView);
                Intent resultValue = new Intent();

                resultValue.PutExtra(AppWidgetManager.ExtraAppwidgetId, widgetId);
                SetResult(Result.Ok,resultValue);
                Finish();
            };
        }        
    }

这是配置布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">   
    <Spinner
        android:id="@+id/widgetSpinner"
        android:layout_width="248dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp" />
    <Button
        android:id="@+id/widgetConfigButton"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:text="Ok"
        android:layout_margin="10dp" />
</LinearLayout>

0 个答案:

没有答案