[英]How to keep 'pressed' look of a button after pressing it?
a regular button changes its look, when it's pressed. 当按下时,常规按钮会改变其外观。 How can i keep this 'pressed' look on the button even after it is released?
即使它被释放,我怎么能在按钮上保持这种“按下”的外观?
Possible solution if You don´t want to use ToggleButton, is to set boolean values in onClickListener 如果您不想使用ToggleButton,可能的解决方案是在onClickListener中设置布尔值
private boolean isPressed = false;
mYourButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(){
if(isPressed==false){
mYourButton.setBackgroundResource(R.drawable.your_pressed_image);
isPressed=true;
}else if(isPressed==true){
mYourButton.setBackgroundResource(R.drawable.your_default_image);
isPressed=false;
}
}
});
There is someways to do it, i suggest by drawable and layouts files. 有一些事情要做,我建议通过drawable和layouts文件。
For example, you have a view where you have a " SEND " or " FINISH BUTTON ", so that view in the folder layout is something like this: 例如,您有一个视图,其中有“ SEND ”或“ FINISH BUTTON ”,因此文件夹布局中的视图是这样的:
<ImageButton
android:id="@+id/btnIdNext"
android:contentDescription="@string/someDescriptionOfImage"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:src="@drawable/buttons_src"
android:background="@drawable/buttons"
android:onClick="someaction" />
As you can see you got two importants drawables, the src and the background. 你可以看到你有两个重要的drawables,src和背景。 So, lets create that files
所以,让我们创建那些文件
In the folder drawable we create the buttons_src.xml file 在drawable文件夹中,我们创建了buttons_src.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/finalizar_active" android:state_pressed="true"/>
<item android:drawable="@drawable/finalizar"/>
</selector>
In the folder drawable we create the buttons.xml file too 在drawable文件夹中,我们也创建了buttons.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_purple_active" android:state_pressed="true"/>
<item android:drawable="@drawable/bg_purple"/>
</selector>
what we got is four images, two for the unpressed state and two for the pressed state. 我们得到的是四个图像,两个用于未按下状态,两个用于按下状态。
The previews next: 预览下一个:
*Unpressed Button http://i.stack.imgur.com/UZMtt.png *未压缩按钮http://i.stack.imgur.com/UZMtt.png
*Pressed Button http://i.stack.imgur.com/1E0u4.png *按下按钮http://i.stack.imgur.com/1E0u4.png
You can use a ToggleButton instead of a regular one, which saves it states after it gets pressed. 您可以使用ToggleButton而不是常规的ToggleButton ,它会在按下后保存状态。
just assign it a pressed and unpressed textures using a selector
, and it will save it pressed texture after you press it. 只需使用
selector
为其分配按下和未按下的纹理,按下它后它将保存按下的纹理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.