问题描述
我在布局
创建一个按钮
。在绘制对象
文件夹,我创建了一个 XML文件
的 btn01_state
。该 btn01_state.xml
分配给按钮
我创建通过安卓背景= @绘制/ btn01_state
现在,在按钮
有一个默认的图片
img1.when我点击
在按钮
时,此搜索变为IMG2,有一次我松开点击的鼠标按钮,图像2又变再IMG1。
就是我想要做的是,要改变按钮的图像埃维点击。的
为例,最初btn01有img01
如果btn01是pressed ==>的btn01设置IMG为img02,并保持img02直到btn01是pressed了。现在,btn01有img02就可以了。
当btn01是pressed,设置img01为btn01。
我希望这澄清了更多的什么,我想做的事情。
btn_selector
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:可绘制=@可绘制/ android_blue
机器人:STATE_ pressed =真/>
<项目机器人:可绘制=@可绘制/ ic_launcher
机器人:state_focused =真/>
<项目机器人:可绘制=@可绘制/ ic_launcher/>
的main.xml
<按钮
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:ID =@ + ID / btn01
机器人:背景=@可绘制/ btn01_state/>
您可以轻松在code做到这一点。
布尔是pssed $ P $ = FALSE;
button.setOnClickListener(buttonListener);
OnClickListener buttonListener =新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
如果(是pressed)
button.setBackgroundResource(R.drawable.icon1);
其他
button.setBackgroundResource(R.drawable.icon2);
!是pressed =是pressed;
}
};
I created a button
in the layout
. In the Drawable
folder I created a XML file
named btn01_state
. The btn01_state.xml
is assigned to the button
i created through "android:background=@drawable/btn01_state
"
Now, the button
has a default image
img1.when i click
on the button
, the image1 changes to img2, and once i release the clicked mouse button, the image2 again changed to img1 again.
what i want to do is,to change the image of the button with evey click.
for an example, initiallybtn01 has img01
if btn01 is pressed==> set img of btn01 to img02 and keep img02 till the btn01 is pressed again. Now, btn01 has img02 on it.
When btn01 is pressed, set img01 to btn01.
I hope this clarified more what i want to do.
btn_selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/android_blue"
android:state_pressed="true" />
<item android:drawable="@drawable/ic_launcher"
android:state_focused="true" />
<item android:drawable="@drawable/ic_launcher" />
main.xml
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btn01"
android:background="@drawable/btn01_state"/>
You can do it easily within the code.
boolean isPressed = false;
button.setOnClickListener(buttonListener);
OnClickListener buttonListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(isPressed)
button.setBackgroundResource(R.drawable.icon1);
else
button.setBackgroundResource(R.drawable.icon2);
isPressed = !isPressed;
}
};
这篇关于如何改变与每一个点击按钮的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!