繁体   English   中英

按下时如何保存图像按钮的状态?

[英]How to save imagebutton's state when pressed?

我创建了一个图像按钮,按下后将更改为另一个图像。 但是,当我按下后退按钮或切换到landscape ,图像按钮将返回其默认图像。

我估计我需要在按下时保存图像的状态?

有人可以帮忙吗?

ib1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if(isPressed)
            ib1.setBackgroundResource(R.drawable.star_pressed);

        else
            ib1.setBackgroundResource(R.drawable.star_unpressed);
            isPressed = true;
    }
});

这样做可以改变方向

  <activity android:name=".Youractivity"

        android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
    >

    </activity>

在清单文件中添加波纹管代码

<activity
      android:name="YourActivity"
      android:configChanges="orientation|screenSize|keyboardHidden"
      android:theme="@style/AppTheme.NoActionBar" //if you need
      android:windowSoftInputMode="stateHidden" //if you need
 />

您可以将按钮状态放在sharedpreferences并在onResume活动方法中获取按钮状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM