2011-05-18 160 views
0

你好我有很多按钮与OnClickListener相同,按钮有不同的颜色,我怎样才能获得按下按钮的颜色(或颜色资源)?android获取颜色

这里是我的代码使用

// declare a OnClickListener that will execute different actions 
     // depending on the view that was clicked 
     View.OnClickListener colorButtonListener = new View.OnClickListener(){ 
      public void onClick (View v){ 
       textarea_note.setBackgroundDrawable(v.getBackground());//set edit background the same of the button 
       dialog.dismiss(); 
      } 
     }; 

     Button button1 = (Button) dialog.findViewById(R.id.button1); 
     Button button2 = (Button) dialog.findViewById(R.id.button2); 
     Button button3 = (Button) dialog.findViewById(R.id.button3); 
     Button button4 = (Button) dialog.findViewById(R.id.button4); 
     Button button5 = (Button) dialog.findViewById(R.id.button5); 
     Button button6 = (Button) dialog.findViewById(R.id.button6); 
     Button button7 = (Button) dialog.findViewById(R.id.button7); 
     Button button8 = (Button) dialog.findViewById(R.id.button8); 
     Button button9 = (Button) dialog.findViewById(R.id.button9); 


     /*for changing the colour when the user clicks on a button*/ 
     button1.setOnClickListener(colorButtonListener); 
     button2.setOnClickListener(colorButtonListener); 
     button3.setOnClickListener(colorButtonListener); 
     button4.setOnClickListener(colorButtonListener); 
     button5.setOnClickListener(colorButtonListener); 
     button6.setOnClickListener(colorButtonListener); 
     button7.setOnClickListener(colorButtonListener); 
     button8.setOnClickListener(colorButtonListener); 
     button9.setOnClickListener(colorButtonListener); 


     /**for the round corner*/ 
     Resources res = this.getResources(); 
     button1.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x1))); 
     button2.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x2))); 
     button3.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x3))); 
     button4.setBackgroundDrawable(this.Sd(res.getColor(R.color.color2x1))); 
     button5.setBackgroundDrawable(this.Sd(res.getColor(R.color.color2x2))); 
     button6.setBackgroundDrawable(this.Sd(res.getColor(R.color.color2x3))); 
     button7.setBackgroundDrawable(this.Sd(res.getColor(R.color.color3x1))); 
     button8.setBackgroundDrawable(this.Sd(res.getColor(R.color.color3x2))); 
     button9.setBackgroundDrawable(this.Sd(res.getColor(R.color.color3x3))); 



     //now that the dialog is set up, it's time to show it  
     dialog.show(); 

回答

0

据我所知,你可以得到的颜色值(例如:R.color.green),但你可以得到一个按钮的绘制对象。

button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
      Drawable d = v.getBackground() 
      } 
     }); 
+0

你有mispeled错误?是的,但一旦我有Drawable如何提取颜色的字符串形式,或颜色ID或任何我可以用来保存到文件和读取和应用到EditText,谢谢 – max4ever 2011-05-18 10:14:04

+0

拼写错误是无用的,你可以禁用它。现在使用:你可以保存可绘制文件本身,并将其设置为任何你想要的视图的背景。但是我担心你可以从这个可绘制对象中获取颜色标识或任何字符串形式。 – mudit 2011-05-18 10:29:10

+0

你的意思是“你不能得到颜色ID或...”或你“可以得到颜色ID或...” – max4ever 2011-05-18 10:35:56

0

我发现这个方法,但我从来没有尝试:

int color = v.getSolidColor(); 
+0

这个函数“返回 该视图的已知纯色背景,或0,如果颜色可能会变化” 我总是得到0 :( – max4ever 2011-05-18 10:15:57

0
TextView txt = (TextView)findViewById(R.id.txtColor); 
txt.setText("Color Of Button "); 
View.OnClickListener colorButtonListener = new View.OnClickListener(){ 
     public void onClick (View v){ 
      txt.setTextColor(v.getSolidColor()); 
      textarea_note.setBackgroundDrawable(v.getBackground());//set edit background the same of the button 
      dialog.dismiss(); 
      Log.i("Color of Button","Color = "+v.getSolidColor()); 
     } 
    }; 

注:请点击此链接:getSolidColor()

在你的代码,尝试更换:

button1.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x1))); 

与:

button1.setBackgroundRessource(R.color.color1x1); 

下载这个项目,我已经创建了4个按钮,和我得到的背景颜色),享受它

Get Color of Buttons

希望它可以帮助

+0

05-18 10:19:51.279:INFO /颜色的按钮(4832):颜色= 0 05-18 10:19:41.408:信息/颜色的按钮(4832):颜色= 0 ,我试过了,但对于不同的颜色我总是颜色= 0 – max4ever 2011-05-18 10:20:01

+0

看我的编辑帖子 – Houcine 2011-05-18 10:22:13

+0

我改变了你说的代码,仍然颜色= 0 – max4ever 2011-05-18 10:35:11