自定义按钮onDraw setBackgroundColor



为了在按钮上显示自定义的背景色,我子类化了Button。

我只需要显示一个充满绿色或红色的正方形,无论与服务器的连接是否建立。

我选择View作为Button,因为我想重新绘制onClick的颜色。

运行此代码后,自定义按钮的背景色始终是第一个设置的颜色。

我在应用程序中登录了多个地方,我不知道为什么它不做我想要的。

在连接状态发生变化时调用ConnectionButtonView.invalidate()。我记录了这一点,并得到了正确的输出:在连接状态改变后,Log.d("conn", ""+activity.isWAMPConnected());打印了连接的正确状态。

我想删除这个类,如果有可能改变一个按钮(视图)的背景编程。到目前为止,我还不能做到这一点。

public class ConnectionButtonView extends Button {
    FullscreenActivity activity;
    public ConnectionButtonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        activity = (FullscreenActivity) context;
    }
    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        int color;
        Log.d("conn", ""+activity.isWAMPConnected());
        if(activity.isWAMPConnected())
            color = 0xFF00FF00;
        else
            color = 0xFFFF0000;
        setBackgroundColor(color);
    }
}

谢谢!

更新:一些日志

D/conn﹕ true
D/de.tavendo.autobahn.WebSocketReader﹕ created
D/de.tavendo.autobahn.AutobahnReader﹕ created
D/de.tavendo.autobahn.WebSocketReader﹕ running
D/de.tavendo.autobahn.AutobahnConnection﹕ reader created and started
D/de.tavendo.autobahn.WebSocketWriter﹕ created
D/de.tavendo.autobahn.AutobahnWriter﹕ created
D/de.tavendo.autobahn.AutobahnConnection﹕ writer created and started
D/conn﹕ true
D/conn﹕ true
D/dalvikvm﹕ GC_FOR_ALLOC freed 9128K, 36% free 33777K/52496K, paused 29ms, total 29ms
D/de.tavendo.autobahn.WebSocketConnection﹕ opening handshake received
D/wamp﹕ connected to ws://86.127.137.166:2014/pubsub
D/de.tavendo.autobahn.AutobahnReader﹕ invalid WAMP message: missing array close or invalid additional args
D/conn﹕ true
D/de.tavendo.autobahn.AutobahnConnection﹕ WAMP session 539a38de65f7c established
D/de.tavendo.autobahn.WebSocketReader﹕ run() : ConnectionLost
D/de.tavendo.autobahn.WebSocketReader﹕ ended
D/de.tavendo.autobahn.WebSocketConnection﹕ fail connection [code = 3, reason = WebSockets connection lost
D/de.tavendo.autobahn.WebSocketReader﹕ quit
D/de.tavendo.autobahn.WebSocketWriter﹕ ended
D/wamp﹕ disconnected from ws://86.127.137.166:2014/pubsub
D/de.tavendo.autobahn.WebSocketConnection﹕ worker threads stopped
D/conn﹕ false
D/conn﹕ false

更新:在从包含视图更改上下文并返回到它之后,背景颜色就ok了。为什么?怎么做?

尝试setBackgroundColor(Color.parse("#FF00FF00"))setBackgroundColor(Color.parse("#FFFF0000"))代替,我认为你的问题是整数。

此外,您应该能够从类外部执行此操作,可能不需要扩展按钮。

您不需要创建子类来完成此操作。Button继承自View, View有setBackgroundColor(int color)方法,所以你应该能够在适当的地方从Activity调用这个方法。你可以在这里看到文档

下面是一个可以调用的示例函数:

 //setup your button from its layout in xml
 private Button button;
 protected void onCreate(Bundle savedInstanceState) {
         super.onCreate();
         setContentView(R.layout.content_layout_id);
         button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });
     }
//change its color if it is connected or not
private void checkWAMP(){
 if(activity.isWAMPConnected()){
    button.setBackgroundColor(Color.GREEN);
  }
 else{
   button.setBackgroundColor(Color.RED);
 }
}

为什么它不起作用: super.onDraw()是实际绘制按钮,包括它的背景。如果你不调用它,setBackgroundColor()将没有效果。但是要小心,因为setBackgroundColor()调用invalidate()本身,这可能会导致堆栈溢出。

我认为当状态改变时从类外改变背景是一个更好的主意。如果你有一个方法调用时,这种情况发生(显然你有,因为你正在调用invalidate(),那么只需改变那里的背景。

简而言之,只要你有这段代码:

myButton.invalidate();

=

myButton.setBackgroundColor(isWAMPConnected() ? color1 : color2);

并使myButton成为一个标准的Button小部件,而不是您的自定义类

相关内容

  • 没有找到相关文章

最新更新