方向(在 configChanges 处)更改使我的按钮在方向后停止工作
我有这个简洁的应用程序...
在 onCreate 时,它相应地绘制“startscreen.xml”:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.startscreen);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.window_title);
在我的应用程序中,我在清单中有“configChanges=”orientation“”。 我有,
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.startscreen);
}
startscreen.xml 既是横向布局又是纵向布局,两者是分开的。
所以对于这个问题,应用程序加载正常,按钮工作。我切换方向,按钮停止响应...
他们都使用relativelayout...
任何线索,有人吗? :S
编辑:
由于您的评论,现在尝试了此操作,但没有结果:S
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.startscreen);
add_note.setOnClickListener(new View.OnClickListener() {
public void onClick(final View v) {
createNote();
}
});
}
您的意思是 onclickListener 还是这些:
add_note = (Button) findViewById(R.id.addnote);
? :S
I have this neat app...
At onCreate, it draws the "startscreen.xml" accordingly to :
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.startscreen);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.window_title);
And in my app, i have "configChanges="orientation" " in the manifest.
And i have,
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.startscreen);
}
startscreen.xml is both a landscape and portrait-layout, both are separated.
So to the problem, the app loads fine, buttons work. I switch orientation, the button stops responding...
They are both using RelativeLayout...
Any clues, anyone? :S
EDIT:
Tried this now thanks to your comments, but no result :S
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.startscreen);
add_note.setOnClickListener(new View.OnClickListener() {
public void onClick(final View v) {
createNote();
}
});
}
Do you mean that onclickListener or these :
add_note = (Button) findViewById(R.id.addnote);
?? :S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要在
onCreate
中向按钮添加点击侦听器,则需要向在onConfigurationChanged
中创建的新按钮对象添加侦听器。您可以考虑在 XML 文件中使用
android:onClick
属性,而不是在代码中设置点击侦听器。这是避免这个问题的好方法。要从 XML 使用
android:onClick
,请执行以下操作:然后在您的 Activity 中定义方法:
android:onClick
方法的名称是完全任意的;它只需将 Activity 中的方法与正确的签名进行匹配 (void(View)
)。当发生点击时,将调用该方法,其方式与为OnClickListener
调用onClick
方法完全相同。使用
android:onClick
时,您不需要调用setOnClickListener
(甚至在onCreate,除非您需要它用于其他用途)。
If you are adding click listeners in
onCreate
to your buttons, you need to add listeners to the new button objects created inonConfigurationChanged
.You might consider using the
android:onClick
attribute in your XML files instead of setting click listeners in code. That's a nice way of avoiding this problem.To use
android:onClick
from XML, do something like this:Then in your activity, define the method:
The name of the
android:onClick
method is totally arbitrary; it just has to match a method in your activity with the proper signature (void <method_name>(View)
). The method will be called when there's a click in exactly the same manner that theonClick
method would be called for anOnClickListener
.When using
android:onClick
, you do not need to callsetOnClickListener
(or even retrieve a reference to the button inonCreate
, unless you need it for something else).