Android中自定义Preference - 晨小主

时间:2024-01-31 14:39:43

Android中自定义Preference

一、需求

开发横屏设备的app时,发现preference显示的都是上下结构,因此需要自定义preference实现横屏显示。

二、layout实现

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="@dimen/space_horizontal"
android:layout_gravity="center"
android:gravity="start"
android:textColor="@color/text_font_black"
android:textSize="@dimen/font_size_prompt"/>

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/space_horizontal"
android:layout_gravity="center"
android:button="@drawable/selection_switch_check_box_background"
android:visibility="gone"/>

<TextView
android:id="@+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/space_horizontal"
android:layout_gravity="center"
android:gravity="end"/>

</LinearLayout>

三、EditTextPreference

public class EditTextPreferenceFix extends EditTextPreference {

public EditTextPreferenceFix(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
getEditText().clearFocus();
hideSysInput();
}

@Override
protected View onCreateView(ViewGroup parent) {
super.onCreateView(parent);
return LayoutInflater.from(getContext()).inflate(R.layout.layout_preference,
parent, false);
}

@Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(R.id.title);
titleView.setText(getTitle());
TextView summaryView = (TextView) view.findViewById(R.id.summary);
summaryView.setText(getSummary());
}

private void hideSysInput() {
Window window = ((Activity) getContext()).getWindow();
View contentView = window.findViewById(Window.ID_ANDROID_CONTENT);

if (contentView.getWindowToken() != null) {
ViewUtils.hideSystemKeyboard(getContext(), contentView);
}
}
}

四、ListPreference

public class ListPreferenceFix extends ListPreference {

public ListPreferenceFix(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
hideSysInput();
}

@Override
protected View onCreateView(ViewGroup parent) {
super.onCreateView(parent);
return LayoutInflater.from(getContext()).inflate(R.layout.layout_preference,
parent, false);
}

@Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(R.id.title);
titleView.setText(getTitle());
TextView summaryView = (TextView) view.findViewById(R.id.summary);
summaryView.setText(getSummary());
}

private void hideSysInput() {
Window window = ((Activity) getContext()).getWindow();
View contentView = window.findViewById(Window.ID_ANDROID_CONTENT);

if (contentView.getWindowToken() != null) {
ViewUtils.hideSystemKeyboard(getContext(), contentView);
}
}
}

五、CheckBoxPreference

public class CheckBoxPreferenceFix extends CheckBoxPreference {

public CheckBoxPreferenceFix(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected View onCreateView(ViewGroup parent) {
super.onCreateView(parent);
return LayoutInflater.from(getContext()).inflate(R.layout.layout_preference,
parent, false);
}

@Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(R.id.title);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);
TextView summaryView = (TextView) view.findViewById(R.id.summary);
titleView.setText(getTitle());
checkBox.setVisibility(View.VISIBLE);
checkBox.setChecked(isChecked());
summaryView.setText(isChecked() ? getSummaryOn() : getSummaryOff());
checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
summaryView.setText(isChecked ? getSummaryOn() : getSummaryOff());
setChecked(isChecked);
});
}
}

六、XML文件

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<com.pax.view.ListPreferenceFix
android:defaultValue="@string/wifi"
android:dialogTitle="@string/commParam_menu_comm_mode_choose"
android:entries="@array/commParam_menu_comm_mode_list_entries"
android:entryValues="@array/commParam_menu_comm_mode_values_list_entries"
android:key="@string/COMM_TYPE"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/commParam_menu_comm_mode"/>
<!-- 打开一个sub screen 移动网络-->
<PreferenceCategory
android:persistent="false"
android:title="@string/communication_menu_mobile">
<com.pax.view.CheckBoxPreferenceFix
android:defaultValue="true"
android:key="@string/MOBILE_KEEP_ALIVE"
android:summaryOff="@string/no"
android:summaryOn="@string/yes"
android:title="@string/commParam_menu_mobile_wifi_keep_alive"/>
<com.pax.view.EditTextPreferenceFix
android:capitalize="words"
android:key="@string/MOBILE_TEL_NO"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/commParam_menu_mobile_dial_no"/>
    </PreferenceCategory>
</PreferenceScreen>
 
posted on 2017-08-21 20:31  晨小主  阅读(1482)  评论(0编辑  收藏  举报