對勾選聲音進行設置
1.進入聲音設置,勾選“選擇操作音”;
2.勾選文件或文件夾時觀察是否有選擇操作音;此時應該會對應的有選擇操作音的,但爲什麼無選擇操作音?
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false">
</CheckBox>
原來SoundManager對於CheckBox的check事件不感冒,只對click事件產生作用,而對於CheckBox有setOnCheckedChangeListener的API,但是沒有setOnClickListener,那CheckBox如何響應onClick事件呢?有一個屬性可以在XML中可以配置。
CheckBox的API描述是這樣的:
1.When the user selects a checkbox, the CheckBox object receives an on-click event.
To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
2.The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:
•Be public
•Return void
•Define a View as its only parameter (this will be the View that was clicked)
故此時需要修改配置文件:
<CheckBox
android:id="@+id/CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:onClick="sound">
</CheckBox>
然後在加載該控件對應的配置文件的Activity中實現該函數,這裏直接爲空函數即可,注意這個函數必須是public權限,返回值爲void,指定唯一參數View。
public void sound(View view){
}
OK,大功告成!此時勾選就可以聽到清脆的勾選聲了。
但是在實際的項目中,由於編譯選擇的方式可能爲user模式,此時user模式對編譯進行了優化,該sound函數只是一個空函數,在編譯檢查的時候發現其未被調用,就直接被優化掉了,在sound函數加一行Log編譯不同的user和eng版本,查看是否有Log輸出,就可以知道了,那優化掉了,這裏點擊checkbox此時就會報錯,因爲找不到該函數。
解決方法,在編譯.mk文件中添加下列語句將優化禁用掉,再編譯任何模式的版本就不會有問題了。
LOCAL_PROGUARD_ENABLED :=disabled