2013-08-01 69 views
2

我已經創建了動畫切換和它的作品完美的,如果android:checked="true"即:它起着提拉1-9,但如果android:checked="false"其停留在繪製10,但它應該從10-20玩。它完美的作品從此但當應用程序啓動它有上述問題。 (關閉)當它停留在10時,它看起來仍然處於開啓狀態。動畫切換按鈕

///////////////////

btn_toggle.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:drawable="@drawable/animatedbutton_on" /> 
      <item android:state_checked="false" android:drawable="@drawable/animatedbutton_off" /> 
</selector> 

/////////// ///////

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ToggleButton 
     android:id="@+id/ToggleButton01" 
     android:layout_width="174dp" 
     android:layout_height="235dp" 
     android:background="@drawable/btn_toggle_bg" 
     android:checked="false" 
     /> 

//////////////////////////

btn_toggle_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+android:id/background" android:drawable="@android:color/transparent" /> 
    <item android:id="@+android:id/toggle" android:drawable="@drawable/btn_toggle" /> 
</layer-list> 

</LinearLayout> 

//////////

animated_button_on.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" > 
    <item android:drawable="@drawable/1" android:duration="200" /> 
<item android:drawable="@drawable/2" android:duration="200" /> 
<item android:drawable="@drawable/3" android:duration="200" /> 
    <item android:drawable="@drawable/4" android:duration="200" /> 
<item android:drawable="@drawable/5" android:duration="200" /> 
    <item android:drawable="@drawable/6" android:duration="200" /> 
<item android:drawable="@drawable/7" android:duration="200" /> 
<item android:drawable="@drawable/8" android:duration="200" /> 
    <item android:drawable="@drawable/9" android:duration="200" /> 
</animation-list> 

///////////// ///

animatedbutton_OFF.xml

<?xml version="1.0" encoding="utf-8"?> 
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" > 
     <item android:drawable="@drawable/a14" android:duration="200" /> 
    <item android:drawable="@drawable/a15" android:duration="200" /> 
     <item android:drawable="@drawable/a16" android:duration="200" /> 
    <item android:drawable="@drawable/a17" android:duration="200" /> 
    <item android:drawable="@drawable/a18" android:duration="200" /> 
     <item android:drawable="@drawable/a19" android:duration="200" /> 
     <item android:draw 

able="@drawable/a20" android:duration="200" /> 
</animation-list> 

///////////////

CustomToggleButtonDemo.java

public class CustomToggleButtonDemo extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     ToggleButton tg=(ToggleButton)findViewById(R.id.ToggleButton01); 
     tg.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       // TODO Auto-generated method stub 

       if (isChecked) { 
         // The toggle is enabled 
        Toast.makeText(getBaseContext(), "ON", 5).show(); 
        } else { 
         // The toggle is disabled 
         Toast.makeText(getBaseContext(), "off", 5).show(); 
        } 

      } 
     }); 

回答

0

你必須重新命名從 「animatedbutton_off.xml」 「animatedbutton_OFF.xml」 文件和從「animatedbutton_on.xml」上的「animated_button_on.xml」

+0

謝謝您的回覆。爲什麼要重命名xml作品?請解釋。 –

+0

這不僅僅是將它重命名爲小寫。它具有像數字開頭,大寫字母和連字符等限制。 – Saqib