【问题标题】:setBackgroundColor() not working on view when checkbox is checked or button is pressed选中复选框或按下按钮时,setBackgroundColor()不适用于视图
【发布时间】:2011-05-27 01:10:52
【问题描述】:

我正在尝试做到这一点,以便每当检查复选框时,视图是一种颜色,当未选中它是另一个颜色,但是当我选中该框时,视图变黑,然后当我检查或取消选中时什么都不会发生盒子。

活动:

package fsg.dev.test.checkboxtest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.view.View.OnClickListener;
import android.widget.SeekBar;

public class checkBoxTest extends Activity implements OnClickListener {
CheckBox checkbox;
SeekBar seekbar;
View view;
Button button;

private static final String TAG = "Checkbox Test";

@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    checkbox = (CheckBox) findViewById(R.id.checkbox);
    seekbar = (SeekBar) findViewById(R.id.seekbar);
    view = (View) findViewById(R.id.background);
    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(this);
    checkbox.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            final String bool;
            if(((CheckBox) v).isChecked()){
                bool = "True";
                Log.d(TAG, bool);
                setBlue();
            } else {
                bool = "False";
                Log.d(TAG, bool);
                setDefault();
            }
        }
    });
}

@Override
public void onClick(View v){
    checkbox.toggle();
    view.setBackgroundColor(R.color.Color);
}

private void setBlue(){
    Log.d(TAG, "In setBlue()");
    view.setBackgroundColor(R.color.Color);
}

private void setDefault(){
    Log.d(TAG, "In setDefault()");
    view.setBackgroundColor(R.color.Default);
}
}

ma​​in.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox 
    android:text="Background Color" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:checked="false"
    android:id="@+id/checkbox">
</CheckBox>
<Button
    android:text="Background Color" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button">
</Button>
<SeekBar 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:id="@+id/seekbar" 
    android:layout_margin="10dip" >
</SeekBar>
<View
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/Default"
    android:id="@+id/background">
</View>

ma​​nifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="fsg.dev.test.checkboxtest"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    <activity android:name=".checkBoxTest"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

谁能弄清楚我做错了什么?

【问题讨论】:

  • 你从 logcat 中得到了什么?方法是否按预期调用?
  • 我没有收到来自 logcat 的警告或错误,并且我知道在跟踪所有函数时一切都按预期调用。我尝试了下面的答案,解决了我的问题。

标签: java android colors checkbox


【解决方案1】:

更改方法 setBlue()setDefault() ,使用: YourActivity.this.setBlue()YourActivity.this.setDefault();

注意:在view.setBackgroundColor(R.color.yourColor);将其替换为:view.setBackgroundColor(Color.BLUE) ;

【讨论】:

  • 嘿,感谢您的快速响应......它就像一个魅力。虽然我确实将view.setBackgroundColor(Color.Blue) 更改为view.setBackgroundResource(R.color.Color),以便我仍然可以使用自定义颜色。
猜你喜欢
  • 2019-06-17
  • 1970-01-01
  • 2015-12-09
  • 2014-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多