安卓小部件:添加新项目 ( "author" ) 文本视图



大家好,我正在开发一个简单的android小部件("Quotes")。该项目工作良好,但我想添加一个新的textview,显示具体的"思想"的作者。我在widget.xml (layout)....中创建了一个新的textview我是个新手!!

代码如下:

WidgetQuotes.Java

  import android.app.PendingIntent;
  import android.appwidget.AppWidgetManager;
  import android.appwidget.AppWidgetProvider;
  import android.content.Context;
  import android.content.Intent;
  import android.widget.RemoteViews;
   public class WidgetQuotes extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    // Build the intent to call the service
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
    RemoteViews remoteViews2 = new RemoteViews(context.getPackageName(), R.layout.widget);
    Intent intent2 = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
    // To react to a click we have to use a pending intent as the
    // onClickListener is
    // excecuted by the homescreen application
    PendingIntent pendingIntent = PendingIntent.getService(
            context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent);

    PendingIntent pendingIntent2 = PendingIntent.getService(
            context.getApplicationContext(), 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews2.setOnClickPendingIntent(R.id.widget_textview2, pendingIntent2);

    // Finally update all widgets with the information about the click
    // listener
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews2);
    // Update the widgets via the service
    context.startService(intent);
    context.startService(intent2);
}
@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
}
public void onReceive1(Context context, Intent intent2) {   
    super.onReceive(context, intent2);
}

}

UpdateWidgetService.Java

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;

   import android.app.Service;
   import android.appwidget.AppWidgetManager;
   import android.content.Intent;
   import android.content.res.AssetManager;
   import android.os.IBinder;
   import android.util.Log;
   import android.widget.RemoteViews;
   public class UpdateWidgetService extends Service {
private static final String TAG = UpdateWidgetService.class.getSimpleName();
@Override
public IBinder onBind(Intent arg0) {
    return null;
}
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Log.d(TAG, "onStart started");
    // Create some random data
    Random random = new Random();
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
    int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
    if (appWidgetIds.length > 0) {
        for (int widgetId : appWidgetIds) {
            List<String> qList = getListFromTxtFile("quotes.txt");
            int nextInt = random.nextInt(qList.size());
            RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
            remoteViews.setTextViewText(R.id.widget_textview, qList.get(nextInt));
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
        stopSelf();
    }
    super.onStart(intent, startId);
}
public List<String> getListFromTxtFile(String txtFileName){
//  File sdcard = Environment.getExternalStorageDirectory();
// Get the text file
// File file = new File(sdcard,txtFileName);
AssetManager am = this.getAssets();
List<String> qList = new ArrayList<String>();
//Read text from file
try {
    InputStream is = am.open("quotes.txt");
          //BufferedReader br = new BufferedReader(new FileReader(file));
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line;
     // get data in text file line by line
    while ((line = br.readLine()) != null) {
        Log.d("line",line);
       qList.add(line);
    }
}
catch (IOException e) {
    //You'll need to add proper error handling here
    {Log.d("Error","There are an error");}
}
return qList;
}
   }

Widget.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:orientation="vertical"
android:background="@drawable/widget_frame3"
android:layout_gravity="center">
<TextView
    android:id="@+id/widget_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|center_horizontal"
    android:layout_marginLeft="80dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30dp"
    android:padding="10dip"
    android:scrollbars="vertical"
    android:text="@string/widget_text"
    android:textColor="@android:color/white" />
<TextView
    android:id="@+id/widget_textview2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|center_horizontal"
    android:layout_marginLeft="80dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="20dp"
    android:padding="10dip"
    android:scrollbars="vertical"
    android:text="@string/widget_text2"
    android:textColor="@android:color/white" />
    </LinearLayout>

我认为你不能在小部件中创建文本视图。在小部件

中只能点击和触摸。

最新更新