亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android ToggleButton 詳解及實例代碼

 更新時間:2017年02月19日 09:30:47   投稿:lqh  
這篇文章主要介紹了Android ToggleButton 詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下

Android ToggleButton 詳解

在Android的開發(fā)過程中,對于ToggleButton的使用頻率也是相當(dāng)?shù)母叩?,下面我就來說一下,這個組件的兩種使用方式。

第一種是簡單的使用,利用Toast的方式彈出提示語句

需要注意的是要想自定義ToggleButton的顯示的內(nèi)容,就需要設(shè)置其TextOn和TextOff的內(nèi)容。

<ToggleButton
    android:id="@+id/toggleButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/toggleButton2"
    android:layout_alignBottom="@+id/toggleButton2"
    android:textOn="開"
    android:textOff="關(guān)"
    android:layout_alignRight="@+id/imageview"
    android:text="Simple test" />

然后是主要的顯示代碼:

case R.id.toggleButton1:
      if(SimpleTest.isChecked()){
        Toast.makeText(getApplication(), "你打開了開按鈕", Toast.LENGTH_SHORT).show();
      }else{
        Toast.makeText(getApplication(), "你打開了關(guān)按鈕", Toast.LENGTH_SHORT).show();
      }
      break;
      //應(yīng)該注意的是,先聲明ToggleButton并初始化,然后注冊偵聽方法

接下來是一個較為復(fù)雜一點的使用案例,那就是配合ImageView來實現(xiàn)不同的圖片顯示狀態(tài)

<ToggleButton
    android:id="@+id/toggleButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageview"
    android:layout_alignParentTop="true"
    android:layout_marginTop="46dp"
    android:textOn="美女"
    android:textOff="圖標(biāo)"
    android:text="With Image" />
 <ImageView 
    android:id="@+id/imageview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/note"
    android:layout_below="@id/toggleButton2"
    />

然后是活動代碼

case R.id.toggleButton2:
      if(WithImage.isChecked()){
        imageview.setImageResource(R.drawable.note);
      }else{
        imageview.setImageResource(R.drawable.ic_launcher);
      }
      break;

需要注意的是,我們同樣需要先進行聲明,才能對其使用,否則會報空指針的錯誤。

下面是程序運行之后的結(jié)果

總結(jié)與設(shè)想:

在使用過程中使用到的ToggleButton 一般來說不會這么的簡單,但是主要的思想和框架還是基于這里的。我們可以在相關(guān)的偵聽方法中添加比如靜音的處理,或者status的改變等等。這樣,我們的應(yīng)用就會變得更加的靈活了。

相關(guān)文章

最新評論