gpt4 book ai didi

java - 如何以编程方式设置焦点/悬停按钮颜色?

转载 作者:行者123 更新时间:2023-11-29 23:19:42 25 4
gpt4 key购买 nike

我使用这段代码来生成一些按钮:

public void addButton(int channel, int start, int length, String texte)
{
RelativeLayout.LayoutParams lprams = newRelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

int top = MARGE_SUPERIEUR + HAUTEUR_LIGNE * channel;
int left = MARGE_GAUCHE + PIXELMINUTE * start;


lprams.setMargins(left,top,0,0);

Button Button = new Button(this.context);
Button.setText(texte);
Button.setMaxHeight(10);
Button.setTextSize(10);
Button.setLayoutParams(lprams);
Button.setWidth(length*PIXELMINUTE);

rLayout.addView(Button);

Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("Clicked", "Clicked" + channel + start);
}
});

}

它工作正常,但我想在我聚焦/悬停其中一个按钮时更改颜色。有什么想法吗?

最佳答案

您可以利用 onFocusChangeListener() 来监听 onFocus()

btn.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus) {
//Toast line...
}
}

});

改变按钮颜色:

btn.setBackgroundColor(Color.WHITE);btn.setTextColor(Color.BLACK);

关于java - 如何以编程方式设置焦点/悬停按钮颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54576216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com