-1

嘿伙计们,我想编写一个显示特定日期倒计时的应用程序。最重要的是,我想提供一个小部件。主要活动还可以,但我不知道如何在主要活动中引用 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);
        }
    }
}

是否可以在小部件的主 Activity 中显示电视?:)

来自德国的许多问候;)

4

1 回答 1

0

您将像这样引用 Textview..

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

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

于 2013-08-23T20:15:42.477 回答