【发布时间】:2017-05-22 07:26:44
【问题描述】:
我正在编写一个代码,当方向改变时改变 TextView 的文本 我正在使用 onConfigurationChanged() 监听器来更改文本
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
val orientation : Int = getResources().getConfiguration().orientation
val oTag = "Orientation Change"
if(orientation == (Configuration.ORIENTATION_LANDSCAPE)){
mytext?.setText("Changed to Landscape")
Log.i(oTag, "Orientation Changed to Landscape")
}else if (orientation == (Configuration.ORIENTATION_PORTRAIT)){
mytext?.setText("Changed to Portratit")
Log.i(oTag, "Orientation Changed to Portratit")
}else{
Log.i(oTag, "Nothing is coming!!")
}
}
但即使在日志中也没有发生任何事情
我也在清单文件中的我的活动中添加了这个属性
android:configChanges="orientation"
我在这里缺少什么? 还有其他方法可以实现吗?
【问题讨论】:
-
在 kotlin 中你需要使用:mytext?.text = "Changed to Landscape" 而不是这个 mytext?.setText("Changed to Landscape")
-
@TerrilThomas 问题是 onConfigurationChanged fun is not called
-
mytext?.setText() 在 onCreate 方法中工作。问题是 onConfigurationChanged() 没有被调用
-
不要在 kotlin 中使用 settext 这样的错误检查链接stackoverflow.com/questions/44096838/…
标签: android orientation kotlin onconfigurationchanged