【问题标题】:Change layout background button press更改布局背景按钮按下
【发布时间】:2016-01-21 19:02:33
【问题描述】:

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

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

【问题讨论】:

  • 好吧,它就是这么做的。但问题是什么?
  • stackoverflow.com/a/2895650/5456493 告诉它是否工作?
  • 你想改变点击按钮的背景吗?
  • 问题是这两种颜色都不起作用,只起作用一种颜色,我想知道如何让你每次按下按钮都放一种不同的颜色,红色和绿色。
  • 颜色没有变化是你的问题吗?或者你想改变颜色,否则不起作用?

标签: android background


【解决方案1】:

在你的听众中:

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 进行比较。

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

【讨论】:

    【解决方案2】:

    谢谢,但我已经解决了:

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

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

    【讨论】:

    • 不同的图像在哪里?您想将背景更改为图像而不是颜色(红色和绿色)(意味着将 mealLayout 背景设置为图像)?
    • 非常感谢,但我成功了,只需添加:mimagen.setBackgroundResource(R.drawable.imagen1);不知道你是不是做android的程序员?
    • 完美,因为有时我排班很麻烦,有些东西不能做,我想付钱请一个专家程序员来做这个工作,如果你有兴趣可以联系我,什么价格你按小时收费。
    • 感谢这个机会,但我不得不说不。因为首先我不认为自己是 Android 专家,其次我已经有一份全职的开发工作,所以我可能没有时间做其他工作,但再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多