Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Thread / Async / GUI- Review - Nur Drüberschauen und Meinungen

Empfohlene Antworten

Veröffentlicht

Hallo Liebe Community,

ich bin grad dabei eine Art minispiel nachzumachen, weil mich die Werbung im Spiel Selber nervt.

Das Spiel wird im Android Studio mit der Version 2.3 Programmiert.

Ich bin selber .NET Entwickler und tu mich sehr schwer. BEispielsweise mit der Formatierung der {} und dass ich die OnClickMethoden selber schreiben muss und es kein Doppelklick auf den Button für mich tut aber egal.

Die Basis ist simpel. Ich habe einen Button und ein Linear Layout (Siehe Bild)

Klicke ich auf den Button, ändert sich nach und nach die Farbe der Buttons.

Jetzt wollte ich nur Fragen, ob das so gut gelöst ist. Weil es gibt ja auch noch Timer, Asynctask, usw. in Java / Android. Wo sind da die Unterschiede? Oder kann ich beim "klassischem" Thread bleiben?

Der Code Funktioniert, ich will nur von erfahrenen Leuten hören ob ich den so lassen kann oder da Optimierungsbedarf ist (Das ist die Mini Basis, da soll noch viel mehr Assynchron laufen mit dem GUI Thread)

Aufruf:


    public void OnButtonClick(View v)

    {

        LinearLayout layout = (LinearLayout) findViewById(R.id.linHorButtons);

        SetColorThread._layout = layout;

        SetColorThread._activity = this;


        SetColorThread thread = new SetColorThread();

        thread.start();

    }

Klasse "SetColorThread"


public class SetColorThread extends Thread

{

    public static Activity _activity;


    public static LinearLayout _layout;


    public static Button _curBtn;


    @Override

    public void run()

    {

        for (int i = 0; i < _layout.getChildCount(); i++)

        {

            Button btn = (Button)_layout.getChildAt(i);

            _curBtn = btn;

             _activity.runOnUiThread(new Runnable()

             {

                 @Override

                 public void run()

                 {

                     _curBtn.setBackgroundColor(Color.GREEN);

                 }

             });

            try

            {

                sleep(100);

            } catch (InterruptedException e)

            {

                e.printStackTrace();

            }

        }

    }

}



Ich freue mich auf Ehrliches Feedback!

Vielen lieben Dank!

post-42683-14430449831993_thumb.png

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.