从小部件中的Main方法显示TextView

时间:2013-08-23 20:07:06

标签: java android eclipse widget

嘿伙计们,我想编写一个显示特定日期倒计时的应用程序。最重要的是,我想提供一个小部件。主要活动是好的,但我不知道如何在主要活动中引用TextView。我会告诉你代码(抱歉,我是初学者:))

主:

public class MainActivity extends Activity {

    private static final String TAG = MainActivity.class.getSimpleName();

    TextView tv;
    long diff;
    long milliseconds;
    long endTime;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/PRICEDOW.TTF");
    tv = (TextView)findViewById(R.id.textView1);
    tv.setTypeface(tf);

    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
    formatter.setLenient(false);

............................................... .......................................

窗口小部件:

public class ExampleAppWidgetProvider extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            TextView tu = (TextView) findViewbyId(R.id.textView1);

            // Get the layout for the App Widget 
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget1);

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

是否可以从小部件中的主要活动中显示电视? :)

来自德国的许多问候;)

1 个答案:

答案 0 :(得分:0)

您将像这样引用Textview ..

TextView tv = (TextView) findViewById(R.id.mytextview);

mytextview将是布局上textview的id,默认情况下为textview1。