- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个有问题和答案选项的测验应用程序。单击错误答案(按钮)时,该特定按钮的文本颜色会变为红色,因为它应该如此。但是,当单击右键时,该特定按钮的文本颜色应变为绿色,稍等片刻,然后继续下一个问题。
我花了几天时间试图弄清楚为什么它不起作用,用谷歌搜索答案,但我仍然无法让它起作用。
我试过使用 Handlers 和 Runnables,但还是不行。
我已经发布了下面 Activity 的完整代码。
请帮忙!
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.question_layout);
Intent i = getIntent();
subject = (Subject) i.getSerializableExtra(Globals.SUBJECT);
questions = subject.getQuestions();
subjectId = subject.getSubjectId();
appPreferences = new AppPreferences();
livesLeft = 3;
index = 0;
maxQuestions = questions.size();
maxIndex = maxQuestions - 1;
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(maxQuestions);
answer1 = (Button) findViewById(R.id.buttonAnswer1);
answer2 = (Button) findViewById(R.id.buttonAnswer2);
setQuestionsAndAnswers(index);
TextView textView = (TextView) findViewById(R.id.textViewSubjectName);
textView.setText(subject.getSubjectName());
}
private void setQuestionsAndAnswers(int index)
{
currentQuestion = questions.get(index);
// Set question
TextView textViewQuestion = (TextView) findViewById(R.id.textViewQuestion);
textViewQuestion.setText(currentQuestion.getQuestion());
// Set correct answer
correctAnswer = currentQuestion.getCorrectAnswer();
sourceUrl = currentQuestion.getSourceUrl();
sourceText = currentQuestion.getSourceText();
// Set answer #1
initializeButton(answer1, currentQuestion.getAnswer1());
// Set answer #2
initializeButton(answer2, currentQuestion.getAnswer2());
// Set source
TextView textViewSource = (TextView) findViewById(R.id.textViewSource);
textViewSource.setText(sourceText);
textViewSource.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
textViewSource.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(sourceUrl));
startActivity(browserIntent);
}
});
// Update progress
updateProgress(index);
}
private void initializeButton(Button button, String answer)
{
button.setText(answer);
button.setTextColor(Color.WHITE);
button.setBackgroundColor(Color.TRANSPARENT);
button.setEnabled(true);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
onClickContent(view);
}
});
}
private void onClickContent(View view)
{
Context context = getApplicationContext();
final Button button = (Button) view;
String answer = button.getText().toString();
if (answer.equalsIgnoreCase(correctAnswer))
{
button.setTextColor(Color.GREEN);
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if (index == maxIndex)
{
appPreferences.editPreferences(context, Globals.PREFERENCE_KEY + subjectId, true);
playSound(Globals.SOUND_QUIZ_COMPLETED);
Toast toast = Toast.makeText(context, "Quiz \"" + subject.getSubjectName() + "\" finished",Toast.LENGTH_LONG);
toast.show();
finish();
} else
{
playSound(Globals.SOUND_CORRECT_ANSER);
// Go to next question
index++;
setQuestionsAndAnswers(index);
}
} else
{
if (livesLeft == 1)
{
playSound(Globals.SOUND_GAME_OVER);
Toast toast = Toast.makeText(context, "Game over", Toast.LENGTH_LONG);
toast.show();
finish();
} else
{
playSound(Globals.SOUND_WRONG_ANSWER);
button.setTextColor(Color.RED);
button.setEnabled(false);
livesLeft--;
TextView lives = (TextView) findViewById(R.id.textViewLives);
lives.setText("Lives: " + livesLeft);
}
}
}
private void updateProgress(int progress)
{
progressBar.setProgress(progress++);
}
private void playSound(int songId)
{
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), songId);
mp.start();
}
最佳答案
首先,Thread.sleep(1000);
在你的例子中运行在 MAIN 线程上,导致 MAIN 线程 hibernate ,不建议这样做。
您可以view.postDelayed
延迟加载下一个问题,
private void onClickContent(View view)
{
Context context = getApplicationContext();
final Button button = (Button) view;
String answer = button.getText().toString();
if (answer.equalsIgnoreCase(correctAnswer))
{
button.setTextColor(Color.GREEN);
if (index == maxIndex)
{
appPreferences.editPreferences(context, Globals.PREFERENCE_KEY + subjectId, true);
playSound(Globals.SOUND_QUIZ_COMPLETED);
Toast toast = Toast.makeText(context, "Quiz \"" + subject.getSubjectName() + "\" finished",Toast.LENGTH_LONG);
toast.show();
finish();
} else
{
playSound(Globals.SOUND_CORRECT_ANSER);
// Go to next question
index++;
// Go to next question after 1000ms
view.postDelayed(new Runnable() {
public void run() {
setQuestionsAndAnswers(index);
}
}, 1000); //here delays 1000ms
}
}
...
}
关于java - Android按钮文字颜色不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29885684/
我有 2 个表 t1,其中存储价格和项目类型以及一个 ENUM 值和 t2,其中存储销售数量。我有一个计算总销售额(价格 * 数量)的查询,但我需要计算两个结果数字之间的差值。 两个数之差的计算公式应
我在使用 zsh 运行 Big Sur 11.2 的 Mac 上更改运行的 java 版本时遇到问题。我想将 Java 1.8 设置为默认设置,因此我使用以下命令设置 JAVA_HOME: $ uns
在下面的代码中,当我改变选择时,会有一个alert。我试图使该功能类似于当我单击 option 时它会显示 alert。 $(document).ready(function() { $("#x"
好吧,我正在制作一个简单的 react 游戏,我的代码如下。现在,当调用 startGame() 时,如果我注释掉 while 循环,一切都会发生,但是当我使用 thread.sleep(1000)
我目前正在尝试将 .plist 文件中的 CFBundleURLTypes 的值更改为新值,但似乎没有任何效果,并且如果我调用,旧值仍然存在 NSLog(@"%@", [[NSBundle mainB
我正在尝试使用 Jsoup 中的解析函数来解析 html 字符串。 我尝试阅读了很多相关的帖子以及API,但仍然没有找到答案。 String html = ""others"; Docume
我正在尝试为水平列表项关闭设置动画alpha 动画有效,layoutparam 值也随时间减少但出于某种原因,这并没有改变列表项的实际高度。 @Override protected void appl
双管道等于: MY_CONST ||= 42 运算符定义? MY_CONST = 42 unless defined? MY_CONST 为什么第二种方式更为普遍?第一个有什么缺点吗? 最佳答案 在一
我希望我的站点地址栏在访问子页面时不要更改其地址,它应该显示我的 index.html ,即使我进入 tosub 页面。 就像我打开 www.xyz.com我导航到它仍应显示的任何页面 www.xyz
我试图通过将购物车存储在数据库的表中来创建购物车,而不是使用内置的 CI 购物车类。我使用 sessionID 来识别一个访问者和另一个访问者。但是我发现sessionID在一段时间内一直在变化,连访
我面临一个奇怪的问题。一旦更改了.java文件-看来测试仍在使用该文件的早期版本(.class)。 从我的 Angular 来看,应该从.build文件夹中删除该类,并从新版本的.java文件重新编译
下面的R代码显示了一个关于向量组合的例子,之后,向量“c”的属性变成了字符。如何让它仍然保持属性为数字? a='aa';b='bb';c=c(1,2,3,4,5) dd=data.table(cbin
我一直在使用 Kinect 和语音识别,我注意到我无法让 AudioLevel 成员从我的 SpeechRecognitionEngine 对象中更改。 我成功地让命令被识别,所以我知道 kinect
大家好,我已经实现了通知功能。我对通知 ID 有疑问。 这是我的代码: protected void ShowNotification(String title, String text){ Noti
所以我第一次正确设置了 UIView 图层的阴影(它显示得非常好)但是当我尝试更改颜色时(没有别的,只是 layer.shadowColor)它不会在 View 中更新。它仍然显示原始颜色。我已经尝试
使用 java 我想在一个程序中生成一些随机值,然后在每次执行第二个程序时在其他程序中使用这些值。 这样做的目的是生成一次随机值,然后在以后每次运行程序时保持并保持它们不变。有可能吗?谢谢 最佳答案
我有几个组件,所有组件都继承了 JCheckBox 而没有覆盖它的任何内容,存储在一个 vector 中,然后遍历该 vector 并将每个组件添加到一个对话框中 CreateLists(); //
我有一个 UITabBarController。现在我想在其上方添加一个 View ,并在用户切换到另一个选项卡时使 View 保持不变。我尝试创建一个 UIViewController,并将 UIT
我正在使用 curses 来打印一个非常漂亮的控制台 UI,我需要它依赖于终端大小。为此,我阅读了 here ,我可以使用 shutil.get_terminal_size .所以我正在做这段代码:
我有一个jsfiddle here . fiddle 中有许多标记文本框。在文本框中输入一个数字,然后继续单击“添加问题”按钮。您将看到剩余的总分等于 10,但它不会改变。剩余分数的数量应该通过减去附
我是一名优秀的程序员,十分优秀!