使按钮留在Android SDK中pssed $ P $ [英] Make buttons remain pressed in Android SDK
问题描述
我有5个简单的按钮,我希望他们能够表现得像单选按钮,但没有复选框(或不管他们叫他们)。它们在排对齐并欲其中只有一个在一次要被选择,并且当选择一个按钮,它必须保持它的选择的样式。我的布局code是这样的:
I have 5 simple buttons and I want them to behave like radio buttons, but without the tick box (or whatever they call them). They are aligned in row and I want only one of them to be selected at once, and when a button is selected it must keep its selected style. My layout code looks like this:
<Button
android:id="@+id/button1"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_margin="0dp"
android:text="@string/mybutton1" />
<Button
android:id="@+id/button2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button1"
android:text="@string/mybutton2" />
// and so on
我的code:
mybtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mybtn1.setPressed(true);
}
});
所以,我不知道如果我能做到这一点的布局或code。有什么办法使按键留在选中状态(选择风格),当我点击它?
此外,是否有可能将事件指派给所有这些按钮?
So, I don't know if I can do this on the layout or in the code. Is there any way to make the button stay in selected state (and selected style) when I click on it? Also, is it possible to assign the event to all these buttons?
谢谢!
推荐答案
刚刚成立的选定状态在onClickListener:
just set their selected state in the onClickListener:
myButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
//handle click
v.setSelected(true);
}
});
点击的状态就是选择状态,您可以打开和关闭的效果。
the clicked state is just the selected state, which you can toggle on and off for effect.
这篇关于使按钮留在Android SDK中pssed $ P $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!