Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更改布局背景按钮按下_Android_Background - Fatal编程技术网

Android 更改布局背景按钮按下

Android 更改布局背景按钮按下,android,background,Android,Background,我有一个按钮,如果你按住手机振动,如果超过1.5秒,就会改变背景颜色。 但我希望当按下第一个按钮时,颜色变为绿色,如果第二次按下,颜色变为红色,以此类推,按钮按下的次数 我的代码: public class MainActivity extends Activity { public static int MILISEGUNDOS_ESPERA = 1500; private RelativeLayout mealLayout; private ToggleButton

我有一个按钮,如果你按住手机振动,如果超过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);
    }
}

问题是这两种颜色都不起作用,只起作用的是一种颜色,我想知道如何让你每次按下按钮都放上不同的颜色,红色和绿色。

在你的听众中:

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,看看它能给你带来什么,然后找到一种合适的方法将它与一些颜色进行比较

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

在侦听器中:

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,看看它能给你带来什么,然后找到一种合适的方法将它与一些颜色进行比较


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

谢谢,但我已经解决了这个问题:

 if (action == MotionEvent.ACTION_DOWN) {
                esperarYCerrar(MILISEGUNDOS_ESPERA);
                vibrator.vibrate(1500);
            } else if (action == MotionEvent.ACTION_UP) {
                vibrator.cancel();
            }
谢谢你所做的一切。
我想知道最后一件事,除了改变背景外,添加不同的图像很容易?

谢谢,但我已经解决了这个问题:

 if (action == MotionEvent.ACTION_DOWN) {
                esperarYCerrar(MILISEGUNDOS_ESPERA);
                vibrator.vibrate(1500);
            } else if (action == MotionEvent.ACTION_UP) {
                vibrator.cancel();
            }
谢谢你所做的一切。
我想知道最后一件事,除了改变背景外,添加不同的图像很容易?

好的,就是这样。但问题是什么?告诉它是否工作?当你点击按钮时,你想改变按钮的背景吗?问题是这两种颜色不工作,只工作一种颜色,我想知道如何让你每次按下按钮时都放上不同的颜色,红色和绿色。不改变颜色是你的问题吗?或者你想换一种不起作用的颜色?好的,这就是它的作用。但问题是什么?告诉它是否工作?当你点击按钮时,你想改变按钮的背景吗?问题是这两种颜色不工作,只工作一种颜色,我想知道如何让你每次按下按钮时都放上不同的颜色,红色和绿色。不改变颜色是你的问题吗?或者你想改变颜色或者说不工作?不同的图像在哪里?您想将背景更改为图像而不是颜色(红色和绿色)(意思是将mealLayout背景设置为图像)?非常感谢,但我成功地做到了,只需添加:mimagen.setBackgroundResource(R.drawable.imagen1);我想知道你是否是android的程序员?太好了,因为有时候我的日程安排有问题,有些事情无法完成,我想请一位专家程序员来做这项工作,如果你感兴趣,你可以联系我,告诉我你每小时收费多少。谢谢你给我这个机会,但我不得不说不。因为首先我不认为自己是安卓方面的专家,其次我已经有了一份全职的开发工作,所以我可能没有时间做其他工作,但再次感谢不同的形象在哪里?您想将背景更改为图像而不是颜色(红色和绿色)(意思是将mealLayout背景设置为图像)?非常感谢,但我成功地做到了,只需添加:mimagen.setBackgroundResource(R.drawable.imagen1);我想知道你是否是android的程序员?太好了,因为有时候我的日程安排有问题,有些事情无法完成,我想请一位专家程序员来做这项工作,如果你有兴趣,你可以联系我,你每小时收费多少。谢谢你给我这个机会,但我不得不说不。因为首先我不认为自己是安卓方面的专家,其次我已经有一份全职的开发工作,所以我可能没有时间做其他工作,但再次感谢