我正在开发一个android应用程序,其中有一些按钮。这个想法是用按钮上方的问题和按钮上可能的答案创建一个测试。
我尝试从不是UI线程的线程为这些按钮着色,并且着色无效,但是程序没有给出任何错误。在UI线程上运行也是如此。
import IO from 'crocks/IO';
const { div, a, h1, h2, ul, li, button } = require('elementx');
const createHeader = div(
h1(
{class: 'title'},
'This is a title'
),
h2(
{class: 'subtitle'},
'This is a subtitle'
),
div(
{class: 'link'},
a(
{href: 'http://github.com'},
'Github'
)
)
);
document.body.appendChild(createHeader);
目前,我获得了所有出色的输出(步骤1-4),但是按钮并没有变成红色或绿色。
答案 0 :(得分:0)
<?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>
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);
});
}
}
Button
列表: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);
}
});
}
}
点击按钮后,您可以期望:
正确的一个-将为绿色
其余-红色