2016-01-20 187 views
1

我有一个按钮,如果你按住手机震动,如果它通过1.5秒改变背景颜色。 但我希望当按下第一个按钮时将绿色变为绿色,并且如果再次按下变为红色等等,按下按钮的次数。更改布局背景按钮按

我的代码:

public class MainActivity extends Activity { 
    public static int MILISEGUNDOS_ESPERA = 1500; 
    private RelativeLayout mealLayout; 
    private ToggleButton toggle; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mealLayout = (RelativeLayout) findViewById(R.id.layout); 
     final Vibrator vibrator; 
     vibrator = (Vibrator) getSystemService(MainActivity.VIBRATOR_SERVICE); 
     Button btn = (Button) findViewById(R.id.button1); 
     btn.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       int action = event.getAction(); 
       esperarYCerrar(MILISEGUNDOS_ESPERA); 
       if (action == MotionEvent.ACTION_DOWN) { 
        vibrator.vibrate(1500); 
       } else if (action == MotionEvent.ACTION_UP) { 
        vibrator.cancel(); 
       } 
       return true; 
      } 
     }); 
    } 

    public void esperarYCerrar(int milisegundos) { 
     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      public void run() { 
       // acciones que se ejecutan tras los milisegundos 
       finalizarApp(); 
       cambiarcolor(); 
      } 
     }, milisegundos); 
    } 

    /** 
    * Finaliza la aplicación 
    */ 
    public void finalizarApp() { 
     mealLayout.setBackgroundColor(Color.RED); 
    } 
    public void cambiarcolor() { 
     ToggleButton button=(ToggleButton) findViewById(R.id.button1); 

     if (button.isChecked()) 
      mealLayout.setBackgroundColor(Color.GREEN); 
     else 
      mealLayout.setBackgroundColor(Color.RED); 
    } 
} 

的问题是,这两种颜色不工作,只有工作的颜色,我想知道如何在每次按下按钮,就可把不同颜色的时间做,红色和绿色。

+0

最新消息您的问题? – justDroid

+0

好的,就是这样。但问题是什么? – Rohit5k2

+0

http://stackoverflow.com/a/2895650/5456493判断它是否工作? – Abhishek

回答

0

在你的听众:

btn.setOnTouchListener(new OnTouchListener() { 
.... 
ColorDrawable color = (ColorDrawable) yourView.getBackground(); 
int colorId = color.getColor(); 

if(colorId == Color.GREEN) 
    mealLayout.setBackgroundColor(Color.RED); 
else if(colorId == Color.RED) 
    mealLayout.setBackgroundColor(Color.GREEN); 
else 
    //If not green or red set an optional color 
.... 

试试这个。唯一我不确定的是与colorId和Color.x的比较。您可能需要以不同的方式比较这一点。尝试记录colorId以查看它得到的结果,然后找到将其与某种颜色Color.x进行比较的适当方法。

您也可以创建一个布尔值并在您的侦听器中检查它是否为false:将背景设置为绿色并将布尔值设置为true。同样的事情,如果布尔值为true:将背景设置为红色,布尔值设为false。

0

谢谢,但我已经解决了这样:

if (action == MotionEvent.ACTION_DOWN) { 
       esperarYCerrar(MILISEGUNDOS_ESPERA); 
       vibrator.vibrate(1500); 
      } else if (action == MotionEvent.ACTION_UP) { 
       vibrator.cancel(); 
      } 

感谢所有DadoZolic。 我想知道最后一件事,除了改变背景之外,很容易添加不同的图像?

+0

不同的图像在哪里?而不是颜色(红色和绿色),您想要将背景更改为图像(意味着将膳食布局背景设置为图像)? – DadoZolic

+0

非常感谢,但我设法做到了,只需添加:mimagen.setBackgroundResource(R.drawable.imagen1); 我不知道你是否是一名android程序员? –

+0

很高兴你能工作。是的,我确实哈哈你为什么想知道? – DadoZolic