Android以编程方式更改按钮的文本颜色

时间:2015-01-17 17:27:55

标签: android button horizontalscrollview

我在linearlayout horizo​​ntalscrollview中动态创建按钮,然后点击我选择按钮位置。

我想知道如何更改所选按钮的文字颜色?

这是我的代码。

String[] categories = {"SUN","MON", "TUS", "WED", "THU", "FRI", "SAT", "SUN","MON", "TUS", "WED", "THU", "FRI", "SAT"};
private LinearLayout ll;
Button btn;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ll = (LinearLayout) findViewById(R.id.hsvLinearLayout);

    for(int i = 0; i < categories.length; i++) {
        btn = new Button(this);
        btn.setText(categories[i]);
        btn.setBackgroundColor(Color.parseColor("#ffffff"));
        btn.setOnClickListener(buttonClick);
        ll.addView(btn);
        int idx = ll.indexOfChild(btn);
        btn.setTag(Integer.toString(idx));
       // btn.setId(idx);
    }
}

OnClickListener buttonClick = new OnClickListener() {
    public void onClick(View v) {
        String idxStr = Integer.toString(ll.indexOfChild(v));
        //(String)v.getTag();
        Toast.makeText(MainActivity.this, idxStr, 6000).show();
    }
};

6 个答案:

答案 0 :(得分:12)

检查类型并指定文本颜色

 OnClickListener buttonClick = new OnClickListener() {
        public void onClick(View v) {
            String idxStr = Integer.toString(ll.indexOfChild(v));
            if(v instanceof Button){
               ((Button)v).setTextColor(Color.parseColor("#000000"));
            }
            Toast.makeText(MainActivity.this, idxStr, 6000).show();
        }
    };

答案 1 :(得分:2)

试试这个

已编辑的答案

 ((Button)view).setTextColor(Color.parseColor("#000000"));

答案 2 :(得分:2)

请检查以下答案herehere

正如您所看到的,您可以通过为按钮的所有状态创建样式文件,以编程方式和xml来完成。

希望有所帮助

答案 3 :(得分:1)

我只是检查所有已发布的解决方案。没有人工作。

他们也像这样生产error

btnjava.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setTextColor(int)' on a null object reference
<块引用>

真正的解决方案:

第 1 步:当您尝试更改 setTextColor 时,请始终使用 try/catch,以防止应用崩溃。

第 2 步:无论您是否已经定义了 Button,请在 R.id.btnId 代码行之前再次定义(如 setTextColor)。

public class MainActivity extends AppCompatActivity {
        Button btn;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btn=findViewById(R.id.btnId);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // use try/catch for handle any kind of error
                    try {
                        Button btnForTextColorChange= (Button) findViewById(R.id.btnId);
                        // must define Button again before setTexColor code line
                        btnForTextColorChange.setTextColor(getResources().getColor(R.color.white));
                    } catch (Exception e){
                        Log.e(TAG, "Error:"+e);
                    }
                    
                }
            });
    }

[抱歉英语不好]

快乐编码 :)

答案 4 :(得分:0)

这有效:

button.setTextColor(getColor(R.color.blue))

答案 5 :(得分:0)

这对我有用:

btnItem.setTextColor(ContextCompat.getColor(context, R.color.black))
相关问题