保持android按钮选择状态 [英] keep android button selected state
问题描述
我知道这是一个以前被问过很多次的问题,但我似乎无法在我的代码中解决它.我有两个按钮,当按下一个按钮时,我希望将其保持在选中状态,反之亦然.我试过使用 setSelected 和 setPressed 来做它,但我似乎无法让它工作.这是我正在使用的代码:
I know this is a question that has been asked many times before, but I can't seem to solve it in my code. I have two buttons, and when one is pressed, I would like to keep it in its selected state, and vice versa. I have tried doing it using but the setSelected and setPressed, but I can't seem to get it to work. Here is the code I am using:
region_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
objects = category;
adap.notifyDataSetChanged();
proximity_button.setPressed(false);
region_button.setPressed(true);
}
});
proximity_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
objects = proximity;
adap.notifyDataSetChanged();
region_button.setPressed(false);
proximity_button.setPressed(true);
}
});
根据评论,我需要补充一点,我有一个自定义的按钮 xml 背景,并希望保留当前的外观.
Based on the comments, I need to add that I have a custom xml background for the buttons, and would like to retain the current look.
推荐答案
我在 Google 上搜索并找到了这篇 Stack Overflow 帖子:
如何让一个按钮保持按下状态点击后?
I searched on Google and found this Stack Overflow post:
How can i keep one button as pressed after click on it?
mycodes_Button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mycodes_Button.setPressed(true);
return true;
}
});
但是看了评论,很有趣!
But read the comment, it's pretty interesting!
这篇关于保持android按钮选择状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!