单击小组件以更改小部件的文本

时间:2013-12-21 00:24:34

标签: java android

我正在尝试将包含textview的窗口小部件在单击窗口小部件时将其文本更改为随机数。它给出了显示小部件的问题。我有这段代码:

public class WidgetConfig extends AppWidgetProvider{
        public static String RandomClick = "RandomClick";
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            super.onUpdate(context, appWidgetManager, appWidgetIds);
            RemoteViews updateViews;
            ComponentName watchWidget;
            updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            watchWidget = new ComponentName(context, WidgetConfig.class);
            updateViews.setOnClickPendingIntent(R.id.randomnumber,getPendingSelfIntent(context, RandomClick));
            appWidgetManager.updateAppWidget(watchWidget,updateViews);
            Random r = new Random();
            int randomInt = r.nextInt(6);
            String randomStringInt = String.valueOf(randomInt);

            final int N = appWidgetIds.length;
            for (int i=0;i<N;i++){
                int appWidgetID = appWidgetIds[i];
                RemoteViews v = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
                v.setTextViewText(R.id.randomnumber, randomStringInt);
                appWidgetManager.updateAppWidget(appWidgetID, v);
            }
        }

        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(RandomClick)){
                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                RemoteViews updateViews;
                ComponentName watchWidget;
                updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                watchWidget = new ComponentName(context, WidgetConfig.class);
                Random r = new Random();
                int randomInt = r.nextInt(6);
                String randomStringInt = String.valueOf(randomInt);
                updateViews.setTextViewText(R.id.randomnumber, randomStringInt);
                appWidgetManager.updateAppWidget(watchWidget, updateViews);
            }
            super.onReceive(context, intent);
        }

        protected PendingIntent getPendingSelfIntent(Context context, String action){
            Intent intent = new Intent(context, getClass());
            intent.setAction(action);
            return PendingIntent.getBroadcast(context, 0, intent, 0);
        }
    }

其中randomnumber只是widget_layout.xml中的textview:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backgroundshape"
    android:layout_margin="8dp"
    android:onClick="ClickRandom">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:minHeight="56dp"
        android:minWidth="56dp"
        android:gravity="center"
        android:id="@+id/randomnumber"
        android:textSize="24dp"/>
</LinearLayout>

非常感谢。谢谢!

1 个答案:

答案 0 :(得分:1)

我上传了完整的作品。你甚至可以从这个link下载代码。这就是我用过的代码。当你点击小部件时,它会自动生成一个随机的no并更新ui我还上传了一张显示输出的图像。

  

<强> MainActivity.java

 public class MainActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Toast.makeText(this, "App widget ready to be added!",
               Toast.LENGTH_LONG).show();

finish();

}
  

<强> WidgetConfig.java

   public class WidgetConfig extends AppWidgetProvider {


  @Override
 public void onUpdate(Context ctxt, AppWidgetManager mgr,
                    int[] appWidgetIds) {
ComponentName me=new ComponentName(ctxt, WidgetConfig.class);

mgr.updateAppWidget(me, buildUpdate(ctxt, appWidgetIds));
  }

 private RemoteViews buildUpdate(Context ctxt, int[] appWidgetIds) {
   RemoteViews updateViews=new RemoteViews(ctxt.getPackageName(),
                                        R.layout.widget);

Intent i=new Intent(ctxt, WidgetConfig.class);

i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0 , i,
                                            PendingIntent.FLAG_UPDATE_CURRENT);

int v = (int)(Math.random()*6);
String rs = Integer.toString(v);
CharSequence cs = rs.subSequence(0, 1);


updateViews.setTextViewText(R.id.textView1,cs); 
updateViews.setOnClickPendingIntent(R.id.textView1, pi);

updateViews.setOnClickPendingIntent(R.id.background, pi);

return(updateViews);
 }
 }

enter image description here