通过点击一个按钮,改变Android SDK中的背景颜色不工作 [英] Changing background color in Android SDK by clicking a button does not work

查看:157
本文介绍了通过点击一个按钮,改变Android SDK中的背景颜色不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序,它是能够点击按钮之后改变背景颜色,但它不工作

 公共类ChangeBackgroundActivity延伸活动{
/ **当第一次创建活动调用。 * /
    按钮blueButton;
    MYLO的LinearLayout;
    @覆盖
   公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        这款mylo =(的LinearLayout)findViewById(R.layout.main);
        blueButton =(按钮)findViewById(R.id.button1);
        blueButton.setOnClickListener(新OnClickListener(){            公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            myLO.setBackgroundColor(0x0000FF); //蓝色code#0000FF
        }
    });
    }
}


解决方案

尝试用这个,

的main.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s
        机器人:ID =@ + ID / myLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>        <按钮
            机器人:ID =@ + ID /按钮1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_alignParentTop =真
            机器人:layout_marginLeft =64dp
            机器人:layout_marginTop =71dp
            机器人:文字=changeColor/>    < / LinearLayout中>

ChangeBackgroundActivity.java

 公共类ChangeBackgroundActivity延伸活动{
/ **当第一次创建活动调用。 * /
    按钮blueButton;
    MYLO的LinearLayout;
    @覆盖
   公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        这款mylo =(的LinearLayout)findViewById(R.id.myLayout);
        blueButton =(按钮)findViewById(R.id.button1);
        blueButton.setOnClickListener(新OnClickListener(){            公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            myLO.setBackgroundColor(Color.BLUE);        }
    });
    }
}

I have a simple program which is able to change the background color after clicking a button, but it does not work

public class ChangeBackgroundActivity extends Activity {
/** Called when the activity is first created. */
    Button blueButton;
    LinearLayout myLO;
    @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myLO=(LinearLayout)findViewById(R.layout.main);
        blueButton=(Button)findViewById(R.id.button1);
        blueButton.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
            // TODO Auto-generated method stub
            myLO.setBackgroundColor(0x0000FF); //blue color code #0000FF    
        }
    });
    }
}

解决方案

Try With this,

main.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/myLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="64dp"
            android:layout_marginTop="71dp"
            android:text="changeColor" />

    </LinearLayout>

ChangeBackgroundActivity.java

public class ChangeBackgroundActivity extends Activity {
/** Called when the activity is first created. */
    Button blueButton;
    LinearLayout myLO;
    @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myLO=(LinearLayout)findViewById(R.id.myLayout);
        blueButton=(Button)findViewById(R.id.button1);
        blueButton.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
            // TODO Auto-generated method stub
            myLO.setBackgroundColor(Color.BLUE);

        }
    });
    }
}

这篇关于通过点击一个按钮,改变Android SDK中的背景颜色不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆