自定义按钮onDraw setBackgroundColor

时间:2014-06-12 23:02:30

标签: android

我将Button子类化,以便在按钮上显示自定义背景颜色。

我只需要显示一个充满绿色或红色的方块,然后建立与服务器的连接。

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

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

我在应用中记录了多个地方,但我不知道为什么它没有按我的意愿行事。

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

如果可以以编程方式更改Button(View)的背景,我想删除此类。到目前为止,我无法做到这一点。

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

更新: 从包含视图更改上下文并返回到它后,背景颜色正常。 还为什么以及如何?

3 个答案:

答案 0 :(得分:0)

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

此外,您应该可以从课外进行,可能不需要扩展按钮。

答案 1 :(得分:0)

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

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

简而言之,只要您拥有此代码:

myButton.invalidate();

只需交换:

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

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

答案 2 :(得分:0)

您不需要创建子类来执行此操作。 Button继承自View,它具有方法setBackgroundColor(int color),因此您应该只能在适当的位置从Activity调用此方法。 您可以看到documentation here

以下是您可以调用的示例函数:

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

}
相关问题