Update MainSwitch widget.

- Fix the MainSwitchBar overlapping issue.
- Add setTitle method to support string resource id.
- Add setChecked method to fix the state of switch not sync issue.
- Get the resource id of title from xml.

Fix: 178425979
Test: Run robotest and apply the widget in Settings and see the ui
Change-Id: I332b017c6a6e32e5ea3929d4b3c7e81104445bcc
This commit is contained in:
Stanley Wang
2021-02-04 00:08:00 +08:00
parent 6344e2dbe8
commit 85852786bc
3 changed files with 29 additions and 12 deletions

View File

@@ -19,6 +19,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?android:attr/colorBackground"
android:orientation="vertical">
<LinearLayout

View File

@@ -17,6 +17,7 @@
package com.android.settingslib.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
@@ -29,6 +30,7 @@ import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.res.TypedArrayUtils;
import com.android.settingslib.RestrictedLockUtils;
@@ -88,6 +90,17 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
});
setChecked(mSwitch.isChecked());
if (attrs != null) {
final TypedArray a = context.obtainStyledAttributes(attrs,
androidx.preference.R.styleable.Preference, 0 /*defStyleAttr*/,
0 /*defStyleRes*/);
final CharSequence title = TypedArrayUtils.getText(a,
androidx.preference.R.styleable.Preference_title,
androidx.preference.R.styleable.Preference_android_title);
setTitle(title);
a.recycle();
}
}
@Override
@@ -126,7 +139,7 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
/**
* Set the title text
*/
public void setTitle(String text) {
public void setTitle(CharSequence text) {
if (mTextView != null) {
mTextView.setText(text);
}

View File

@@ -18,7 +18,6 @@ package com.android.settingslib.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import androidx.core.content.res.TypedArrayUtils;
@@ -40,7 +39,7 @@ public class MainSwitchPreference extends TwoStatePreference {
private final List<OnMainSwitchChangeListener> mSwitchChangeListeners = new ArrayList<>();
private MainSwitchBar mMainSwitchBar;
private String mTitle;
private CharSequence mTitle;
private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
@@ -81,24 +80,28 @@ public class MainSwitchPreference extends TwoStatePreference {
setLayoutResource(R.layout.main_switch_layout);
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs,
final TypedArray a = context.obtainStyledAttributes(attrs,
androidx.preference.R.styleable.Preference, 0 /*defStyleAttr*/,
0 /*defStyleRes*/);
final CharSequence title = TypedArrayUtils.getText(a,
androidx.preference.R.styleable.Preference_title,
androidx.preference.R.styleable.Preference_android_title);
if (!TextUtils.isEmpty(title)) {
setTitle(title.toString());
}
setTitle(title);
a.recycle();
}
}
/**
* Set the preference title text
*/
public void setTitle(String text) {
mTitle = text;
@Override
public void setChecked(boolean checked) {
super.setChecked(checked);
if (mMainSwitchBar != null) {
mMainSwitchBar.setChecked(checked);
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
if (mMainSwitchBar != null) {
mMainSwitchBar.setTitle(mTitle);
}