问题描述
我知道这是一个已经被问过很多次的问题,但我似乎无法来解决它在我的code。我有两个按钮,当一个是pressed,我想保持选中状态,反之亦然。我曾尝试使用这样做,但使用setSelected并设置pressed,但我似乎无法得到它的工作。这里是code我使用的:
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.
推荐答案
好了,所以我用了互联网的谷歌搜索功能:对,我发现了如下因素:<一href=\"http://stackoverflow.com/questions/4747311/how-can-i-keep-one-button-as-$p$pssed-after-click-on-it\">This threa不要计算器...
Ok, so I used the Google search functionality of the internet :p and i found the folowing :This threa don StackOverflow...
mycodes_Button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mycodes_Button.setPressed(true);
return true;
}
});
不过看了评论,这是pretty有趣!
But read the comment, it's pretty interesting !
这篇关于保持Android的按钮选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!