欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ToggleButton 詳解及實(shí)例代碼

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

Android ToggleButton 詳解

在Android的開(kāi)發(fā)過(guò)程中,對(duì)于ToggleButton的使用頻率也是相當(dāng)?shù)母叩?,下面我就?lái)說(shuō)一下,這個(gè)組件的兩種使用方式。

第一種是簡(jiǎn)單的使用,利用Toast的方式彈出提示語(yǔ)句

需要注意的是要想自定義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="開(kāi)"
    android:textOff="關(guān)"
    android:layout_alignRight="@+id/imageview"
    android:text="Simple test" />

然后是主要的顯示代碼:

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

接下來(lái)是一個(gè)較為復(fù)雜一點(diǎn)的使用案例,那就是配合ImageView來(lái)實(shí)現(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"
    />

然后是活動(dòng)代碼

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

需要注意的是,我們同樣需要先進(jìn)行聲明,才能對(duì)其使用,否則會(huì)報(bào)空指針的錯(cuò)誤。

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

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

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

相關(guān)文章

最新評(píng)論