如何通过单击图像按钮更改窗口小部件背景

时间:2014-01-01 18:45:08

标签: android android-widget widget imagebutton

对,我看了很多问题,答案和教程,但没有人帮助过我,所以我的问题是我最后的希望......

在我的时钟小部件配置活动中,我希望能够拥有图像按钮,以便用户可以更改配置菜单上的背景,就像它在HD Widgets应用程序上一样,但当然具有不同的背景和目前为止我有这个

 @Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.widget_config);
            setResult(RESULT_CANCELED);
 }

    int mAppWidgetId;
    @SuppressWarnings({ "unused" })
    private void showAppWidget() {

       mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
       Intent intent = getIntent();
       Bundle extras = intent.getExtras();
       if (extras != null) {
           mAppWidgetId = extras.getInt(EXTRA_APPWIDGET_ID,INVALID_APPWIDGET_ID);
           final ImageButton button1 = (ImageButton) findViewById(R.id.button1);
           button1.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
        button1.setImageResource(R.drawable.background3);
            AppWidgetProviderInfo providerInfo = AppWidgetManager.getInstance(
                                    getBaseContext()).getAppWidgetInfo(mAppWidgetId);
            String appWidgetLabel = providerInfo.label;
            Intent startService = new Intent(WidgetConfigActivity.this, null);
            startService.putExtra(EXTRA_APPWIDGET_ID, mAppWidgetId);
            startService.setAction("FROM CONFIGURATION ACTIVITY");
            setResult(RESULT_OK, startService);
            startService(startService);
            finish();
            if (mAppWidgetId == INVALID_APPWIDGET_ID) {
            Log.i("I am invalid", "I am invalid");
            finish();
            }
    }

从我看过的答案中,您需要使用RemoteViews,但我不完全确定如何将其实现到我的代码中以便我需要一些帮助 谢谢

1 个答案:

答案 0 :(得分:0)

你得到像这样的RemoteViews

  RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

然后你改变这样的背景

remoteViews.setInt(R.id.some_view, "setBackgroundResource", R.drawable.my_color);