GOOGLE ADS

Freitag, 15. April 2022

Android Studio set Background Color on Button tut nichts

Ich entwickle eine Android-Anwendung, in der es eine Schaltfläche gibt. Die Idee ist, einen Test mit Frage über dem Button und möglicher Antwort auf den Buttons zu erstellen.

Ich habe versucht, diese Schaltflächen aus einem Thread zu färben, der nicht der UI-Thread ist, und die Färbung ist nicht effektiv, aber das Programm gibt keinen Fehler aus. Und das gilt auch für die Ausführung im UI-Thread.

public void discoverAnswer(final String rep){
this.sleeping = true;
Log.d(TAG, "discoverReponse: step1");
btn1.setBackgroundColor(btn1.getText().toString().equals(rep)? Color.GREEN:Color.RED);
btn2.setBackgroundColor(btn2.getText().toString().equals(rep)? Color.GREEN:Color.RED);
btn3.setBackgroundColor(btn3.getText().toString().equals(rep)? Color.GREEN:Color.RED);
btn4.setBackgroundColor(btn4.getText().toString().equals(rep)? Color.GREEN:Color.RED);
Log.d(TAG, "discoverReponse step2");
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "discoverReponse: step3");
btn1.setBackgroundColor(btn1.getText().toString().equals(rep)? Color.GREEN:Color.RED);
btn2.setBackgroundColor(btn2.getText().toString().equals(rep)? Color.GREEN:Color.RED);
btn3.setBackgroundColor(btn3.getText().toString().equals(rep)? Color.GREEN:Color.RED);
btn4.setBackgroundColor(btn4.getText().toString().equals(rep)? Color.GREEN:Color.RED);
Log.d(TAG, "discoverReponse step4");
}
});
}

Im Moment habe ich alle Ausnahmen ausgegeben (Schritt 1-4), aber die Schaltfläche wurde weder rot noch grün.


Lösung des Problems

Okay, also Schritt für Schritt. Mit diesem Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aaaa" />
<Button
android:id="@+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="bbbb" />
<Button
android:id="@+id/button_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="cccc" />
<Button
android:id="@+id/button_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="dddd" />
</LinearLayout>

die dort aussehen:

vor dem Klick

Sie können einfache Logik erstellen:

package training.com.myapplication;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
// All of your buttons
Button btn1;
Button btn2;
Button btn3;
Button btn4;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// "Find" buttons in layout
btn1 = findViewById(R.id.button_1);
btn2 = findViewById(R.id.button_2);
btn3 = findViewById(R.id.button_3);
btn4 = findViewById(R.id.button_4);
// your correct response
String response = "aaaa";
// Adding listeners to all of buttons
btn1.setOnClickListener(v -> {
discoverAnswer(response);
});
btn2.setOnClickListener(v -> {
discoverAnswer(response);
});
btn3.setOnClickListener(v -> {
discoverAnswer(response);
});
btn4.setOnClickListener(v -> {
discoverAnswer(response);
});
}
public void discoverAnswer(final String rep) {
// Change color all of the buttons
runOnUiThread(() -> {
btn1.setBackgroundColor(btn1.getText().toString().equals(rep)? Color.GREEN: Color.RED);
btn2.setBackgroundColor(btn2.getText().toString().equals(rep)? Color.GREEN: Color.RED);
btn3.setBackgroundColor(btn3.getText().toString().equals(rep)? Color.GREEN: Color.RED);
btn4.setBackgroundColor(btn4.getText().toString().equals(rep)? Color.GREEN: Color.RED);
});
}
}

Oder mit einer Liste von Buttons:

package training.com.myapplication;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
// List of buttons
List<Button> listOfButtons = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listOfButtons.addAll(Arrays.asList(
findViewById(R.id.button_1),
findViewById(R.id.button_2),
findViewById(R.id.button_3),
findViewById(R.id.button_4)
));
// your correct response
String response = "aaaa";
// Adding listeners to all of buttons - for API < 24
for (Button button: listOfButtons) {
button.setOnClickListener(v -> discoverAnswer(response));
}
}
public void discoverAnswer(final String rep) {
// Change color all of the buttons
runOnUiThread(() -> {
for (Button button: listOfButtons) {
button.setBackgroundColor(button.getText().toString().equals(rep)? Color.GREEN: Color.RED);
}
});
}
}

Und nachdem Sie auf die Schaltfläche geklickt haben, können Sie Folgendes erwarten:


  • richtige - wird grün sein


  • Rest von ihnen - rot



nach Klick

Keine Kommentare:

Kommentar veröffentlichen

Warum werden SCHED_FIFO-Threads derselben physischen CPU zugewiesen, obwohl CPUs im Leerlauf verfügbar sind?

Lösung des Problems Wenn ich das richtig verstehe, versuchen Sie, SCHED_FIFO mit aktiviertem Hyperthreading ("HT") zu verwenden, ...