1

我有一个包含 aButton和的小部件TextViewButton单击时Activity1将显示,TextView单击Activity2时将显示。此外,当用户返回主屏幕时,文本会添加到TextView.

一切都很好,直到我决定一起更改按钮和文本,然后“BOOM!”

当我更改小部件的xml布局时,一切都颠倒了。我的意思是,当用户单击 时ButtonActivity2将显示,当TextView单击时,Activity1将显示。即使用户返回主屏幕,文本也会显示在Button.

我取消了我的更改,一切都回到了第一种情况,除了文本仍然显示在按钮中。然后我做了Clean & Build但没有任何改变。最后,经过一些测试,我删除了“gen”文件并运行了项目,这次它又回到了初始状态。所以我尝试再次实施相同的更改,但遇到了同样的问题。

我想一起更改Buttonand TextView,但我不知道该怎么做。任何机构都有解决方案?提前致谢。

编辑:添加代码

小部件提供者:

public class DictionWidgetProvider extends AppWidgetProvider {

private RemoteViews views;
private RemoteViews hideViews;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    System.out.println("Update");

    for (int i = 0; i < appWidgetIds.length; i++) {
        int appWidgetId = appWidgetIds[i];

        registeOnClickForElements();
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

@Override
public void onReceive(Context context, Intent intent) {

    if (App.HIDE_WIDGETS.equals(intent.getAction())) {
        hideWidgetInstances();
    } else if (App.UPDATE_WIDGET_STATIC_TEXT.equals(intent.getAction())) {
        String name = "lastWord";
        String lastWord = intent.getExtras().getString(name);
        views = new RemoteViews(context.getPackageName(),
                R.layout.widgetlayout);
        AppWidgetManager mManager = AppWidgetManager.getInstance(App
                .getContext());
        ComponentName cn = new ComponentName(App.getContext(),
                DictionWidgetProvider.class);
        registeOnClickForElements();
        views.setTextViewText(R.id.widgettextview, lastWord);
        mManager.updateAppWidget(cn, views);
    } else {
        super.onReceive(context, intent);
    }

}

private void hideWidgetInstances() {
    hideViews = new RemoteViews(App.getContext().getPackageName(),
            R.layout.hide);
    AppWidgetManager mManager = AppWidgetManager.getInstance(App
            .getContext());
    ComponentName cn = new ComponentName(App.getContext(),
            DictionWidgetProvider.class);
    mManager.updateAppWidget(cn, hideViews);
}

private void registeOnClickForElements() {

    views = new RemoteViews(App.getContext().getPackageName(),
            R.layout.widgetlayout);
    // register on click for text view:
    Intent sampleActivityIntent = new Intent(App.getContext(),
            SampleActivity.class);
    sampleActivityIntent.setAction(App.WIDGET_TEXTVIEW_CLICKED);
    PendingIntent sampleActivityPendingIntent = PendingIntent.getActivity(
            App.getContext(), 0, sampleActivityIntent, 0);
    views.setOnClickPendingIntent(R.id.widgettextview,
            sampleActivityPendingIntent);

    // register on click for "openDictionary" button:
    String category = "requesterFromWidget";
    Intent dictionActivityIntent = new Intent(App.getContext(),
            DictionActivity.class);
    dictionActivityIntent.setAction(App.WIDGET_OPEN_DICTIONARY_CLICKED);
    dictionActivityIntent.addCategory(category);
    PendingIntent dictionActivityPendingIntent = PendingIntent.getActivity(
            App.getContext(), 0, dictionActivityIntent, 0);
    views.setOnClickPendingIntent(R.id.openDictionary,
            dictionActivityPendingIntent);
}

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/widgettextview"
            android:layout_width="160dp"
            android:layout_height="25dp"
            android:text="search" />

        <Button
            android:id="@+id/speaker"
            android:layout_width="30dp"
            android:layout_height="25dp"/>

        <Button
            android:id="@+id/openDictionary"
            android:layout_width="30dp"
            android:layout_height="25dp" />
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:src="@drawable/splitter" />

</LinearLayout>
4

1 回答 1

0

这似乎是一个错误,我解决了我的问题:
我强制更改,然后在 xml 布局中更改了视图的 id 并保存了所有(Ctrl + shift + s在 eclipse 中)。然后我再次将它的值返回到以前的值并保存了所有。

于 2012-06-14T16:30:52.857 回答