可更改的小部件拨号

时间:2012-10-18 17:13:22

标签: android image android-appwidget

到目前为止,我有:

public class Info extends Activity {

    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    public Info() {
        super();
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setResult(RESULT_CANCELED);

        setContentView(R.layout.widget_activity_info);

        findViewById(R.id.save_button).setOnClickListener(mOnClickListener);

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

    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            final Context context = Info.this;

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);


            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.layout.widget_activity);
                    appWidgetManager.updateAppWidget(mAppWidgetId, views);

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

}

基于此,我希望保存按钮成为默认图像,并创建另一个按钮,将更改android:dial =“@ drawable / widgetdial”到android:dial =“@ drawable / widgetdial2”。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

好的,如果我理解正确,您希望在窗口小部件配置活动中有两个按钮(Info)。一个按钮将在窗口小部件中切换图像(R.drawable.widgetdialR.drawable.widgetdial2之间)。另一个按钮将保存选项并退出Info活动。正确的吗?

当然,您需要在Info活动的布局中添加一个按钮。我在下面引用了R.id.change_button作为示例。

我测试了这个并且它有效。我已经为您的代码添加了一些代码,并标记了带有注释的新代码:

public class Info extends Activity {

    //next three lines is new code
    private int drawableSrc;
    private TextView statusText;
    private Button changeButton;

    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    public Info() {
        super();
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setResult(RESULT_CANCELED);

        setContentView(R.layout.widget_activity_info);

        findViewById(R.id.save_button).setOnClickListener(mOnClickListener);

        //next six lines is new code
        statusText = (TextView)findViewById(R.id.text);
        statusText.setText("Now showing widgetdial image");
        changeButton = (Button)findViewById(R.id.change_button);
        changeButton.setText("Change to widgetdial2 image");
        drawableSrc = R.drawable.widgetdial;
        changeButton.setOnClickListener(changeOnClickListener);

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

    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            final Context context = Info.this;

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.layout.widget_activity);

            //next line is new code, needs to come before updateWidgetApp
            views.setImageViewResource(R.id.viewid, drawableSrc);

            appWidgetManager.updateAppWidget(mAppWidgetId, views);

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

    //everything below this is new code
    View.OnClickListener changeOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            if(drawableSrc == R.drawable.widgetdial) {
                drawableSrc = R.drawable.widgetdial2;
                statusText.setText("Now showing widgetdial2 image");
                changeButton.setText("Change to widgetdial image");
            }
            else {
                drawableSrc = R.drawable.widgetdial;
                statusText.setText("Now showing widgetdial image");
                changeButton.setText("Change to widgetdial2 image");
            }
        }
    };
}

我已将您要更改的内容引用为R.id.viewid,但这只是一个示例ID引用。

根据您想要更改的内容(窗口小部件背景或窗口小部件imageview?),您可以在此处查看:Other SO question

以下是我用于Info活动的示例布局(您引用为R.id.widget_activity_info的文件):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />
    <Button
        android:id="@+id/change_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/text" />
    <Button
        android:id="@+id/save_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Save"
        android:layout_below="@id/change_button" />
</RelativeLayout>

如果它仍然不适合您,请查看my example I have uploaded for you。 zip文件包含可以导入到eclipse中的工作区。您可以在模拟器中运行它。

我希望这会对你有所帮助。