2013-06-24 120 views

回答

0

您可以使用ToggleButton而不是普通的,它在按下后会保存它的状态。

只需使用selector为其指定压制和未压缩纹理,并在按下之后将其保存为压制纹理。如果您不想将使用切换按钮

+0

这个。或者考虑使用Switch(用于更新的API) – dberm22

1

可能的解决办法,是在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; 

        } 
       } 
      }); 
1

有someways通过绘制和布局文件,这样做,我建议。

例如,你有,你有一个“SEND”或“完成按钮”视图,所以在文件夹布局的看法是这样的:

<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" /> 

,你可以看到你有两个重要的drawables,src和背景。所以,让我们创建一个文件

在文件夹绘制我们创建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> 

在文件夹绘制我们创建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> 

我们得到的是四个图像,两个用于未压缩状态,两个用于按下状态。

的预览下一个:

*未按下按钮 http://i.stack.imgur.com/UZMtt.png

*按下的按钮 http://i.stack.imgur.com/1E0u4.png