2014-03-26 38 views
0

我想用幻燈片創建開啓/關閉按鈕。 我正在使用這個技術:用幻燈片創建開啓/關閉按鈕

我把按鈕放在相同的位置,一個是可見的,另一個是隱藏的。 當我點擊一個其他按鈕出現,點擊消失。

現在我怎樣才能使該按鈕可以滑動。

下面是按鈕: enter image description here

我怎樣才能做到這一點?

+2

你可以使用[轉](HTTP: //developer.android.com/reference/android/widget/Switch.html)(API> 14)作爲基礎,並使用您自己的圖形創建它的自定義版本。 –

+0

滑動/關閉按鈕-http://stackoverflow.com/questions/8150596/android-how-to-create-slide-on-off-button?rq = 1 –

回答

0

嘗試使用開關,但是,如果你確定minSdkVersion> = 14。

希望以下代碼可以幫助您。

在佈局文件創建一個開關,如:

<Switch 
    android:id="@+id/on_off_switch" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textOff="OFF" 
    android:textOn="ON"/> 

然後在你的活動得到佈局的開關,並設置監聽如下,

Switch onOffSwitch = (Switch) findViewById(R.id.on_off_switch); 
onOffSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(),"isChecked"+isChecked, Toast.LENGTH_LONG).show(); 
    } 
});