本文介绍了Android小工具:添加一个新的项目("作者")的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我开发一个简单的Android窗口小部件(行情)。该项目工作正常,但我想补充一个新的TextView,显示了特定的思想的作者。我创建了widget.xml(布局)的新的TextView ....我是新手!
在这里,code是:
WidgetQuotes.Java
进口android.app.PendingIntent;
进口android.appwidget.AppWidgetManager;
进口android.appwidget.AppWidgetProvider;
进口android.content.Context;
进口android.content.Intent;
进口android.widget.RemoteViews; 公共类WidgetQuotes扩展AppWidgetProvider {@覆盖
公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
super.onUpdate(背景下,appWidgetManager,appWidgetIds); //构建意图调用服务
RemoteViews remoteViews =新的RemoteViews(context.getPackageName(),R.layout.widget);
意向意图=新意图(context.getApplicationContext(),UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds); RemoteViews remoteViews2 =新的RemoteViews(context.getPackageName(),R.layout.widget);
意图intent2 =新意图(context.getApplicationContext(),UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds); //要处理发生的点击,我们必须使用挂起的意图作为
// onClickListener是
//通过主屏幕应用excecuted
的PendingIntent的PendingIntent = PendingIntent.getService(
context.getApplicationContext(),0,意向,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);
//最后有关点击的信息更新所有部件
//监听器
appWidgetManager.updateAppWidget(appWidgetIds,remoteViews); appWidgetManager.updateAppWidget(appWidgetIds,remoteViews2); //更新通过服务窗口小部件
context.startService(意向);
context.startService(intent2);
}@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
super.onReceive(背景下,意图);}
公共无效onReceive1(上下文的背景下,意图intent2){
super.onReceive(背景下,intent2);
}}
UpdateWidgetService.Java
进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;
进口的java.util.List;
进口了java.util.Random; 进口android.app.Service;
进口android.appwidget.AppWidgetManager;
进口android.content.Intent;
进口android.content.res.AssetManager;
进口android.os.IBinder;
进口android.util.Log;
进口android.widget.RemoteViews; 公共类UpdateWidgetService延伸服务{
私有静态最后弦乐TAG = UpdateWidgetService.class.getSimpleName();@覆盖
公众的IBinder onBind(意向为arg0){
返回null;
}@覆盖
公共无效调用onStart(意向意图,诠释startId){
super.onStart(意向,startId); Log.d(TAG,在onStart开始); //创建一些随机数据
随机随机=新的随机(); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext()); INT [] = appWidgetIds intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); 如果(appWidgetIds.length大于0){ 对于(INT为widgetid:appWidgetIds){
清单<串GT;的QList = getListFromTxtFile(quotes.txt);
INT nextInt = random.nextInt(qList.size()); RemoteViews remoteViews =新的RemoteViews(getPackageName(),R.layout.widget);
remoteViews.setTextViewText(R.id.widget_textview,qList.get(nextInt));
appWidgetManager.updateAppWidget(为widgetid,remoteViews);
}
stopSelf();
}
super.onStart(意向,startId);
}公开名单<串GT; getListFromTxtFile(字符串txtFileName){//文件的SD卡= Environment.getExternalStorageDirectory();
//获取文本文件
//文件文件=新的文件(SD卡,txtFileName);AssetManager上午= this.getAssets();清单<串GT;的QList =新的ArrayList<串GT;();//从文件中读取文本尝试{
InputStream为= am.open(quotes.txt);
//的BufferedReader BR =新的BufferedReader(新的FileReader(文件));
BR的BufferedReader =新的BufferedReader(新的InputStreamReader(是));
串线; //获得通过行文本文件中的行数据
而((行= br.readLine())!= NULL){ Log.d(线,线); qList.add(线);
}
}
赶上(IOException异常五){
//你需要在这里添加正确的错误处理 {Log.d(错误,有错误);}
}
返回的QList;} }
Widget.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =160dp
机器人:方向=垂直
机器人:背景=@绘制/ widget_frame3
机器人:layout_gravity =中心><的TextView
机器人:ID =@ + ID / widget_textview
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =左| CENTER_HORIZONTAL
机器人:layout_marginLeft =80dp
机器人:layout_marginRight =10dp
机器人:layout_marginTop =30dp
机器人:填充=10dip
机器人:滚动条=垂直
机器人:文字=@字符串/ widget_text
机器人:文字颜色=@机器人:彩色/白/><的TextView
机器人:ID =@ + ID / widget_textview2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =左| CENTER_HORIZONTAL
机器人:layout_marginLeft =80dp
机器人:layout_marginRight =10dp
机器人:layout_marginTop =20dp
机器人:填充=10dip
机器人:滚动条=垂直
机器人:文字=@字符串/ widget_text2
机器人:文字颜色=@机器人:彩色/白/> < / LinearLayout中>
解决方案
我觉得你不能创建一个小部件文本视图。只有挖掘和触摸有可能在小部件
Everyone, I am developing a simple android widget ("Quotes"). The project is working fine, but I would like to add a new textview that shows the author of the specific "thought". I created a new textview in widget.xml (layout).... I'm a newbie!!!
Here the code is:
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>
解决方案
I Think you can not create a text view in a widget. Only tap and touch is possible in the widget
这篇关于Android小工具:添加一个新的项目(&QUOT;作者&QUOT;)的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!