2013-12-21 73 views
0

我想使包含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只是TextView的在widget_layout.xml:

<?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> 

幫助非常感謝。謝謝!

+0

問題是什麼 –

回答

1

我已經上傳完整的工作。你甚至可以從這個link下載代碼。這是我用過的代碼。當你點擊這個小部件時,它會自動生成一個隨機的no並更新ui.I還上傳了一張顯示輸出的圖像。

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