2014-04-08 102 views
0

當我的按鈕被禁用時,我需要刪除文字陰影效果和按鈕啓用時,我需要再次添加此效果。動態刪除/添加陰影效果

selector_btn.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item 
    android:drawable="@drawable/btn_disabled" 
    android:state_enabled="false" /> 

<item 
    android:drawable="@drawable/btn_pressed" 
    android:state_pressed="true" /> 

<item 
    android:drawable="@drawable/btn_default" /> 

styles.xml

<style name="TextShadow"> 
    <item name="android:textColor">#ffffffff</item> 
    <item name="android:shadowColor">#0D67B9</item> 
    <item name="android:shadowRadius">2.0</item> 
    <item name="android:shadowDy">-2.0</item> 
</style> 

<style name="BigButton" parent="TextShadow"> 
    <item name="android:background">@drawable/selector_btn</item> 
</style> 

回答

1
You have make 2 defferent styles for enable and disable condition and apply it to textview when it disable or vise versa ...      
      <style name="TextShadow_disable"> 
       <item name="android:textColor">#ffffffff</item> 
       <item name="android:shadowColor">#0D67B9</item> 
       <item name="android:shadowRadius">0</item> 
      <item name="android:shadowDy">0</item> 
       </style> 
      <style name="TextShadow_enable"> 
       <item name="android:textColor">#ffffffff</item> 
      <item name="android:shadowColor">#0D67B9</item> 
      <item name="android:shadowRadius">2.0</item> 
      <item name="android:shadowDy">-2.0</item> 
      </style> 

      textstyle = (TextView) findViewById(R.id.mytext); 
      textstyle.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        getTextStyle(); 

       } 
      }); 

寫下此方法來檢查啓用禁用;

 public void getTextStyle() { 
      if(textstyle.isEnabled()){ 
       textstyle.setTextAppearance(this, R.style.TextShadow_enable); 
       } 
      else{ 
        textstyle.setTextAppearance(this, R.style.TextShadow_disable); 
       } 
      } 
+0

非常感謝)它的作品)但我實現了我自己的按鈕並重寫drawableStateChangedmethod()來監聽狀態改變事件。每當狀態發生變化時,都會改變文本樣式。 – user3134124