【问题标题】:Android blinking background edittext?Android闪烁背景edittext?
【发布时间】:2014-04-17 15:44:43
【问题描述】:

我想要一个背景闪烁的编辑文本,例如对于测验,有人在编辑文本中写下答案,按下按钮后,背景应根据答案闪烁红色或绿色。

你有什么想法吗?谢谢:)

【问题讨论】:

标签: android user-interface android-edittext


【解决方案1】:

这就是你需要的吗?

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;
            }
        });
    }
}

它看起来像这样:


当 o 回答错误时:


对于正确的:


我猜它只是按照你的需要工作,抱歉我无法测试它需要视频而不是图片:)

祝你好运,如果有问题请告诉我

【讨论】:

  • 你只是设置了另一种颜色,这里根本没有闪烁
  • 我只是调整我的代码以掩盖他正在寻找的内容我反复添加了一个淡入淡出动画;)
  • 带有淡入淡出动画,文字会和背景一起闪烁,不是吗?
  • 没什么大不了的 :) 我猜他是在征求意见,所以这是我的想法 ;)
【解决方案2】:

用一个简单的处理程序试一试。

//How many times does it blink
private final static int NUM= 10;
//Milliseconds interval
private final static long INTERVAL= 100; 

初始化你的HandlerRunnable

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);

【讨论】:

    【解决方案3】:

    可能有更清洁的解决方案,但您可以使用类似的解决方案。

    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);
    

    【讨论】:

    • while(counter--) 这样的声明在 Java 中是非法的。您不能使用除boolean 之外的任何类型作为条件,它不是 JavaScript!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多