我有一个奇怪的问题。当我运行应用程序并单击按钮(btn_eng 或 btn_pl)时,我看到登录 logcat(“删除对话框”或“删除对话框 PL”)但是下次我单击它时它不会显示并且按钮不会执行它们的 onClick 操作. 我试着制作按钮来改变语言。在我的应用程序中,我有一个包含数据形式资源(字符串数组)的对话框,当我更改语言时,我想删除对话框并使用足够的数据创建新的对话框。
//public static Activity act; -> before onCreate(..)
this.act = this;
Button btn_eng = (Button) findViewById(R.id.btnEN);
btn_eng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("XXX", "Erase dialog");
act.removeDialog(1);
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = act.getBaseContext().getResources().getConfiguration();
config.locale = locale;
act.getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
act.setContentView(R.layout.main);
}
});
Button btn_pl = (Button) findViewById(R.id.btnPL);
btn_pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("XXX", "Erase dialog PL");
act.removeDialog(1);
Locale locale = new Locale("pl");
Locale.setDefault(locale);
Configuration config = act.getBaseContext().getResources().getConfiguration();
config.locale = locale;
act.getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
act.setContentView(R.layout.main);
}
});
最佳答案
这是因为您正在重置 onClick 中的内容 View 。调用 setContentView
不会导致 onCreate
运行 - 这就是(我猜)您链接 onClick
按钮的监听器。但是,它确实会导致显示布局的全新实例。您需要找到一种新方法来使用新数据刷新屏幕,而不是替换屏幕。最好的方法是为您的 Activity 获取包装 ViewGroup
并调用 View#invalidate();
关于android - 单击后按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11489971/