我想要一个带有闪烁背景的edittext,例如对于测验,有人在编辑文本中写下答案,按下按钮后,背景应根据答案闪烁红色或绿色。
你知道吗?感谢:)答案 0 :(得分:0)
就像你需要的那样?
public class MainActivity extends Activity {
int correctAnswer = 12;
EditText answerET;
Button answerBtn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
answerET = (EditText) findViewById(R.id.editText1);
answerBtn = (Button) findViewById(R.id.button1);
//Question for example is 2x6=?
answerBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
int answer = Integer.parseInt((answerET.getText().toString()));
if(answer == correctAnswer){
answerET.setBackgroundColor(Color.GREEN);
answerET.setAnimation(startBlicking());
}else{
answerET.setBackgroundColor(Color.RED);
answerET.setAnimation(startBlicking());
}
}
//blinking animation :)
private Animation startBlicking(){
Animation fadeIn = new AlphaAnimation(1, 0);
fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
fadeIn.setDuration(1000);
fadeIn.setRepeatCount(-1);
return fadeIn;
}
});
}
}
它看起来如何:
当回答错误答案时:
对于正确的:
它只是按照你的需要工作我猜,抱歉我无法测试它需要视频而不是图片:))
祝你好运,如果出现问题,请告诉我
答案 1 :(得分:0)
可能有一个更清洁的解决方案,但你可以使用类似的东西。
private class BlinkTask extends AsyncTask<Integer, Boolean, Boolean> {
protected Long doInBackground(Integer... timeout) {
boolean active = true;
int counter = 10;
// If timeout 1s this will flash for 10s
while(counter-- != 0)
{
try {
Thread.Sleep(timeout[0])
publishProgress(active);
active =! active;
} catch(...){}
}
return true;
}
protected void onProgressUpdate(Boolean... active) {
if(active)
// Change it to one colour here depending on correct answer
mAnswerText.setBackgroundColor(mCorrectAnswer ? Color.GREEN : Color.RED)
else
// Change it to standard colour
}
protected void onPostExecute(Boolean result) {
}
}
使用。
在`onClick'方法中启动它new BlinkTask.execute(1);
答案 2 :(得分:0)
尝试使用简单的处理程序。
//How many times does it blink
private final static int NUM= 10;
//Milliseconds interval
private final static long INTERVAL= 100;
初始化您的Handler
和Runnable
。
Handler handler = new Handler();
Runnable r = new Runnable(){
int aux = 0;
public void run(){
mEditTextView.setBackgroundColor(mCorrectAnswer ? Color.GREEN : Color.RED);
if(aux < NUM)
handler.postDelayed(this, INTERVAL);
else
aux = 0;
}
};
当你想要触发它时,只需:
handler.post(r);