简体   繁体   English

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

[英]Widget on click to change text of widget

I'm trying to make a widget containing a textview change its text to a random number on clicking the widget. 我试图使包含textview的小部件在单击小部件时将其文本更改为随机数。 It gives a problem displaying widget. 显示小部件时出现问题。 I have this code: 我有以下代码:

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);
        }
    }

In which randomnumber is just the textview in widget_layout.xml: 其中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>

Help much appreciated. 帮助非常感谢。 Thank you! 谢谢!

I have uploaded the complete work.You can even download the code from this link .Well this is the code which i have used.When you click on the widget it will automatically generate a random no and update ui.I have also uploaded an image which shows the output also. 我已经上传了完整work.You甚至可以从这里下载代码的链接 。好了,这是我所used.When你点击小工具会自动生成一个随机的不和更新ui.I也已上传的图片的代码这也显示了输出。

MainActivity.java 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 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);
 }
 }

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM