小部件似乎没有注册点击

时间:2012-05-04 18:11:23

标签: android android-widget

我正在尝试使用图像按钮和textview构建一个小部件,当我按下我的图像按钮时,我希望它将文本更改为数组中的随机字符串。我有我认为会起作用但它没有更新我的文本而且不知道为什么。我的imagebutton叫做imagewidgeterica,我的textview叫做widget1label。

继承我的代码:

package kevin.erica.box;

import java.util.Arrays;
import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RemoteViews;
import android.widget.TextView;



public class Widget extends AppWidgetProvider {




private static final Random rgenerator = new Random();
private static final Random rgenerator2 = new Random();
private String[] myString1;






  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
      Resources res = context.getResources();
        myString1 = res.getStringArray(R.array.myArray);


    final int N = appWidgetIds.length;
    Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));
    // Perform this loop procedure for each App Widget that belongs to this
    // provider
    for (int i = 0; i < N; i++) {
      int appWidgetId = appWidgetIds[i];
      // Create an Intent to launch ExampleActivity
      Intent intent = new Intent(context, Widget.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
      // Get the layout for the App Widget and attach an on-click listener
      // to the button
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
      views.setOnClickPendingIntent(R.id.imagewidgeterica, pendingIntent);
      // To update a label
      views.setTextViewText((R.id.widget1label, myString1[rgenerator.nextInt(myString1.length)]);
      // Tell the AppWidgetManager to perform an update on the current app
      // widget
      appWidgetManager.updateAppWidget(appWidgetId, views);
    }
  }
}

清单:

<receiver android:name=".Widget" android:label="Erica's Box">
  <intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  </intent-filter>
 <meta-data android:name="android.appwidget.provider" android:resource="@xml/erica_info" />
</receiver>

1 个答案:

答案 0 :(得分:0)

import java.util.Random;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity {
/** Called when the activity is first created. */

private String[] myString;
private static final Random rgenerator = new Random();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final String[] myString; 

Resources res = getResources();

myString = res.getStringArray(R.array.myArray); 

Button generate = (Button) findViewById(R.id.generate);


generate.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
   String q = myString[rgenerator.nextInt(myString.length)];
   TextView tv = (TextView) findViewById(R.id.textview);
    tv.setText(q);}});
}
}

/res/values/array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="myArray"> 
<item>string 1</item> 
<item>string 2</item> 
<item>string 3</item> 
<item>string 4</item> 
<item>string 5</item>
</string-array> 

</resources>