Android 视图更改 - 按钮状态重置
我有一个基本上用于启动/停止的按钮。因此最初按钮的文本设置为启动。我给它附加了一个 OnClickListener。因此,每当单击它时,我都会更改其文本。因此,如果它是开始,它就会停止,反之亦然。
当我将手机视图从纵向更改为横向或反之亦然时,问题就出现了,按钮文本被重置。
例如,我点击了开始按钮——它变成了停止。现在,如果我倾斜手机以更改视图,按钮文本将设置为重新开始。
我是否以错误的方式使用按钮?
I have a button which is basically used for start/stop. So initially the text of the button is set to start. I attached a OnClickListener to it. So whenever it gets clicked i change its text. So if it was start it become stop and vice-versa.
The problem comes when i change my phone view from portrait to landscape or vice-versa the button text gets reset.
So for example I clicked the start button---it changed to stop. Now if I tilt my phone to change the view the button text gets set to start again.
Am I using the button in a wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该保存按钮状态。当屏幕方向发生变化时,onCreate 会被调用,所有应用程序变量都会重新初始化。在此处阅读更多信息 http://developer.android.com/reference/android/app/ Activity.html
You should save your button state. When screen orientation changes, onCreate is called and all your app variables are re-intitialized. Read more here http://developer.android.com/reference/android/app/Activity.html
不,您正在以正确的方式使用该按钮。
您看到的是“配置更改”。当您倾斜设备时,Android 会重新创建您的 Activity 并重新创建所有视图(因此,它们会获得 XML 中描述的默认标题)。
您需要
android:configChanges="orientation|keyboardHidden"
。如果您有不同的横向和纵向布局,当您需要...有关进一步说明,请参阅本文
No, you are using the button in the right way.
The thing what you are seeing is "configuration change". When you are tilting your device, Android recreating your activity and recreating all it's views (so, they getting default captions as them described in XML).
You need to do the
android:configChanges="orientation|keyboardHidden"
. It is not suitable, if you have different layouts for landscape and portraint orientations, when you need to...See this article for further explanation