Changing background color in Android SDK by clicking a button does not work

孤街醉人 提交于 2019-12-11 01:45:38

问题


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    
        }
    });
    }
}

回答1:


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);

        }
    });
    }
}



回答2:


use

myLO=(LinearLayout)findViewById(R.id.main);

insteadof

myLO=(LinearLayout)findViewById(R.layout.main);

your layout must be like that

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main"
        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"
          />
</LinearLayout>



回答3:


You have to create an xml file(selector file) and put it in drawable folder in res(res->drawable->yourselectorfile.xml. After that set xml file in button's background in your layout file

button_background_selector.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/your_hover_image" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/your_hover_image" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/your_hover_image"/>
    <item android:drawable="@drawable/your_simple_image" />
</selector>

Now set the above file in button's background.

<Button
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:textColor="@color/grey_text"
    android:background="@drawable/button_background_selector"/>

and for changing color use

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:color="#000000" /> <!-- pressed -->
     <item android:state_focused="true"
           android:color="#000000" /> <!-- focused -->
     <item android:color="#FFFFFF" /> <!-- default -->
 </selector>

as your button_background_selector.xml




回答4:


Use this, this worked for me:

YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));


来源:https://stackoverflow.com/questions/11271047/changing-background-color-in-android-sdk-by-clicking-a-button-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!