在Android中,focusable
是一個屬性,用于指示一個視圖是否可以獲得焦點。當一個視圖具有焦點時,它會接收用戶的輸入,如鍵盤輸入、鼠標點擊等。默認情況下,只有具有focusable
屬性的視圖才能獲得焦點。
如果你想擴展focusable
屬性,可以通過以下方法實現:
View
類,并在其中重寫onFocusChanged()
方法。在這個方法中,你可以根據需要處理視圖獲得或失去焦點時的邏輯。例如:public class CustomView extends View {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFocusChanged(boolean gainFocus) {
super.onFocusChanged(gainFocus);
if (gainFocus) {
// 視圖獲得焦點時的處理邏輯
} else {
// 視圖失去焦點時的處理邏輯
}
}
}
CustomView
類,并設置其focusable
屬性。例如:<com.example.myapplication.CustomView
android:id="@+id/custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true" />
通過這種方式,你可以根據需要擴展focusable
屬性,并在自定義視圖中處理焦點相關的邏輯。