我有一个按钮,如果你按住手机震动,如果它通过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);
}
}
的问题是,这两种颜色不工作,只有工作的颜色,我想知道如何在每次按下按钮,就可把不同颜色的时间做,红色和绿色。
最新消息您的问题? – justDroid
好的,就是这样。但问题是什么? – Rohit5k2
http://stackoverflow.com/a/2895650/5456493判断它是否工作? – Abhishek