简体   繁体   中英

Change textview through a button in a widget

I have to create a widget for a project but I'm new to Java and especially to widget.

What I'd like to do is to change a TextView when I click on a button and to choose what I'll show in an array.

Something like this sequence :

  1. Button clicked.
  2. Random number generated.
  3. Choose the sentence corresponding in the array.
  4. Set it in the TextView .

Can you please give me some clues or methods to help me ?

Here is my onUpdate void, I've tried a few thing and it just show the changment I've done in my Sentence() void.

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
    }
    Intent intent = new Intent(context, NewAppWidget.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.new_app_widget);
    remoteViews.setOnClickPendingIntent(R.id.Ln, pendingIntent);
    Sentence();
    remoteViews.setTextViewText(R.id.appwidget_text, sentence);

    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

First, I advice you do follow some tutorial like Button tutorial and java random number generator

Here it's how to handle the click on your button

    //retrieve your button from your xml
    final Button button = (Button) findViewById(R.id.myButton);

    //handle the click on your button
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Update your array
        }
    });

Here is how to generate random number

Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM