Android 单击可更改背景色

Android 单击可更改背景色,android,android-activity,Android,Android Activity,我正在用Android做一个简单的程序,每当我点击空白屏幕,屏幕就会改变颜色。有人能帮我吗。如何在空白屏幕上使用onclick功能。您可以这样设置背景: layout = (RelativeLayout) findViewById(R.id.layoutname); layout.setBackgroundColor(AndroidColor);// can be a drawbale file or a string for colours 然后有几种方法可以通过单击来更改背景。我会在布局中

我正在用Android做一个简单的程序,每当我点击空白屏幕,屏幕就会改变颜色。有人能帮我吗。如何在空白屏幕上使用onclick功能。

您可以这样设置背景:

layout = (RelativeLayout) findViewById(R.id.layoutname);
layout.setBackgroundColor(AndroidColor);// can be a drawbale file or a string for colours
然后有几种方法可以通过单击来更改背景。我会在布局中添加一个ontouch侦听器。如果为true(用户按下屏幕),则调用我上面给出的代码。您需要编辑我给出的内容,因为我不知道您的命名方案,但类似的内容应该可以完成这项工作:

layout .setOnTouchListener(new View.OnTouchListener() {  
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        layout.setBackgroundColor(AndroidColor);
        return true;
    }
});