我有一个自定义布局的对话框,当我按下按钮时我尝试关闭它:
private void showAboutDialog() {
dialog = new Dialog(MainMenu.this);
dialog.setContentView(R.layout.about_dialog);
dialog.setCancelable(true);
dialog.setTitle(R.string.about_title);
dialog.show();
LayoutInflater inflater = (LayoutInflater)
getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.about_dialog,
(ViewGroup) findViewById(R.id.layout_root));
Button closeButton = (Button) layout.findViewById(R.id.about_close_button);
closeButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
dialog.dismiss();
}
});
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
你的听众没有被召唤.
更换:
Button closeButton = (Button) layout.findViewById(R.id.about_close_button);
Run Code Online (Sandbox Code Playgroud)
有:
Button closeButton = (Button) dialog.findViewById(R.id.about_close_button);
Run Code Online (Sandbox Code Playgroud)
并删除上面的两行(LayoutInflater inflater = ...和View layout = ...).
| 归档时间: |
|
| 查看次数: |
3894 次 |
| 最近记录: |