首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据android中的按钮文本值更改按钮背景色

根据android中的按钮文本值更改按钮背景色
EN

Stack Overflow用户
提问于 2013-07-21 13:58:20
回答 1查看 661关注 0票数 0

我已经创建了一个android应用程序,它动态地创建了50个按钮,工作非常完美,但问题是当我按下一个静态定义的按钮时,会将按钮(以文本命名为5)的背景色更改为黄色。

有人能告诉我一些解决方案吗?我的代码如下所示

我的安卓平台是2.3.3

代码语言:javascript
复制
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

public class MyMain extends Activity {
Button change;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        change= (Button) findViewById(R.id.change);  

        setContentView(R.layout.mymain);
        createCalender();
        change.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
              // what to write here in order to change the color of button titled as 5 to yellow as its background color

        }
    });
    }
   public void createCalender()
   {  
    LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT, 1.0f);


    param.setMargins(10, 10, 10, 10);


    LinearLayout rowLayout=null;
    Button[][] buttons = new Button[10][5]; 
    int count=51;
    int tab=1;
    for (int i = 0; i<10; i++) 
    {
        if(count%5==1)
        {
            rowLayout = new LinearLayout(this);
            rowLayout.setBackgroundColor(Color.BLACK);
            rowLayout.setWeightSum(5);
            layoutVertical.addView(rowLayout,param);
            count=count-5;
        }
        for(int j=0;j<5;j++)
        {
            buttons[i][j]=new Button(this);
            buttons[i][j].setText(""+tab);
            buttons[i][j].setHeight(55);
            buttons[i][j].setWidth(80);
            buttons[i][j].setTextColor(Color.BLACK);
            buttons[i][j].setBackgroundColor(Color.GREEN);

            tab++;

            rowLayout.addView(buttons[i][j],param);

        }
    }
}

}
EN

回答 1

Stack Overflow用户

发布于 2013-07-21 14:03:35

试一试如下:

代码语言:javascript
复制
if("5".equals(v.getText())){
    v.setBackgroundColor(Color.YELLOW);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17773098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档