问题描述
我有一个的CustomButton
类(扩展的LinearLayout
),在那里我膨胀,其中包含一个<$ C $布局C>切换按钮(在现实中,这是比较复杂的,但我在这里简单的问题)。
I have a CustomButton
class (extends LinearLayout
) where I inflate a layout which contains a ToggleButton
(in reality this is more complex, but I simplified here the problem).
public class CustomButton extends LinearLayout {
private ToggleButton toggleOnOffButton;
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom_button_layout, this);
}
@Override
protected void onFinishInflate() {
toggleOnOffButton = (ToggleButton) findViewById(R.id.toggle_on_off_button);
super.onFinishInflate();
}
public ToggleButton getToggleOnOffButton() {
return toggleOnOffButton;
}
}
custom_button_layout.xml
custom_button_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton android:id="@+id/toggle_on_off_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Off"
android:textOn="On"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
我有我的地方用充气2 的CustomButton
-s一个布局的活动。
第一个切换按钮的开/关状态保存在共享preferences,我从那里的onCreate
方法装载的价值。
I have an activity where I inflate an layout with 2 CustomButton
-s.The on/off state of the first toggleButton is saved in shared preferences and I load the value from there in onCreate
method.
public class FirstActivity extends Activity
{
private CustomButton customButton;
private ToggleButton toggleBut;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
customButton = (CustomButton)findViewById(R.id.toggleButton);
toggleBut = customButton.getToggleOnOffButton();
boolean saved = loadPreferences("toggleBut");
toggleBut.setChecked(saved);
toggleBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean checked = toggleBut.isChecked();
savePreferences("toggleBut", checked);
}
});
}
private void savePreferences(String key, boolean value){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
private boolean loadPreferences(String key){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
return sharedPreferences.getBoolean(key, true);
}
}
main.xml中
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.example.cs.ssd.custom.CustomButton
android:id="@+id/toggleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<com.example.example.cs.ssd.custom.CustomButton
android:id="@+id/toggleButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
当我启动应用程序的第一个切换按钮接通。当我改变屏幕的方向,自动将第一切换按钮变为关闭,甚至已保存
的值真正
,被称为 toggleBut.setChecked(保存);
,我认为这与做 CutomButton
我如果因为创建的main.xml
布局只包含1 的CustomButton
这个问题不会重现。
我不知道我在做什么错了?
下面是与上面的code中的归档(作为一个项目):
When I start the application the first toggleButton is ON. When I change the orientation of the screen, automatically the first toggleButton become Off, even saved
has value true
and is called toggleBut.setChecked(saved);
and I think this has to do with the CutomButton
I've created because if the main.xml
layout contains only 1 CustomButton
this problem does not reproduce.I'm not sure what I'm doing wrong...Here is the archive with the above code (as a project): archive
推荐答案
如果你希望你的CustomButton保留后改变方向时只需重写的onSaveInstanceState()和onRestoreInstanceState()。其当前状态
If you want your CustomButton to retain its current state after an orientation change simply override onSaveInstanceState() and onRestoreInstanceState().
一个解决方案
我通过你的code跑去,发现onActivityCreated(后变更后 toggleBut
的状态),但在onStart()之前。为了避免上述任何一种方法覆盖您切换设置,我干脆搬离onViewCreated()这些行:
I ran through your code and noticed that toggleBut
's state was being changed after onActivityCreated() but before onStart(). To avoid having any of these methods override your toggle settings, I simply moved these lines from onViewCreated():
boolean saved = loadPreferences("toggleBut");
toggleBut.setChecked(saved);
和把它们放在onResume()。希望帮助!
and put them in onResume(). Hope that helps!
更好的解决方案
当系统试图恢复默认您的切换按钮的设置都被覆盖 saveInstanceState
,大概在Fragment.onActivityCreated()。
Your ToggleButton setting are being overwritten when the system tries to restore the default saveInstanceState
, probably in Fragment.onActivityCreated().
在的CustomButton,覆盖这些功能,像这样:
In CustomButton, override these functions like so:
@Override
protected Parcelable onSaveInstanceState() {
Bundle state = new Bundle();
state.putParcelable("default", super.onSaveInstanceState());
state.putParcelable("toggle", toggleOnOffButton.onSaveInstanceState());
return state;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
Bundle bundle = (Bundle) state;
super.onRestoreInstanceState(bundle.getParcelable("default"));
toggleOnOffButton.onRestoreInstanceState(bundle.getParcelable("toggle"));
};
理解,系统仍然会改变切换按钮的状态,没有一件事。但让我试着解释一下,发生的事:
Understand that the system will still change the ToggleButton states, without the one more thing. But let me try to explain what;s happening:
-
onActivityCreated(捆绑savedInstanceState)
通过它的savedInstanceState
的每一个布局元素通过调用onRestoreInstanceState(捆绑savedInstanceState)` 。 - onRestoreInstanceState()的布局的根元素开始的第一和横穿了布局的层次结构(在这种情况下,它设置每个切换按钮最后的选中状态)。
- 由于默认的方法显然不工作,我们需要定义为的ToggleButtons我们自己保存/恢复方法。否则,我们所做的任何更改的系统调用onRestoreInstanceState(前)将被系统再次改变...
onActivityCreated(Bundle savedInstanceState)
passes it'ssavedInstanceState
to every layout element by calling 'onRestoreInstanceState(Bundle savedInstanceState)`.- onRestoreInstanceState() begins with the layout's root element first and traverses up the layout's hierarchy (in this case it sets the checked state of each ToggleButton last).
- Since the default methods are clearly not working, we need to define our own save / restore method for the ToggleButtons. Otherwise any changes we make before the system calls onRestoreInstanceState() will be changed again by the system...
所以,最后我们将通过添加以下行来CustomButton.onFinishInflate()排除此默认行为的ToggleButtons
So, lastly we will exclude the ToggleButtons from this default behavior by adding the following line to CustomButton.onFinishInflate():
toggleOnOffButton.setSaveEnabled(false);
瞧,您的CustomButtons自动保留其状态。
Voila, your CustomButtons automatically retain their state.
这篇关于切换按钮上的方向改变状态改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!