Android之自定义AlertDialog无法监听控件
参考:http://www.cnblogs.com/511mr/archive/2011/10/21/2220253.html
要做一个自定义的弹出框,以前都是用一个Activity来实现,总觉得不是很好看,弹出的框有时候感觉有点大,所以上网查资料说,可以给AlertDialog自定义一个view就行了,代码如下
AlertDialog dialog = new AlertDialog.Builder(
XXXXActivity.this).create();
dialog.show();//该代码一定要放在给dialog设置自定义view的前面
Window window = dialog.getWindow();
window.setContentView(R.layout.termail_handle_dialog);
这样可以完全展示出你所定义的view,但是该view中我添加了两个ImageView,需要对ImageView设置监听器,我是用下面方法做的,代码如下
LayoutInflater factory = LayoutInflater.from(TermailFetchActivity.this);
final View view = factory.inflate(R.layout.termail_handle_dialog, null);
ImageView scanAddTermail = (ImageView) view.findViewById(R.id.scan_termail);
ImageView writeAddTermail = (ImageView) view.findViewById(R.id.write_termail);
scanAddTermail.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "scanImage", Toast.LENGTH_SHORT).show();
}
});
怎么都监听不到该控件。后来发现网上用下面方法可以监听到。代码如下
Window window = dialog.getWindow();
window.setContentView(R.layout.termail_handle_dialog);
ImageView scanAddTermail = (ImageView) window.findViewById(R.id.scan_termail);
ImageView writeAddTermail = (ImageView) window.findViewById(R.id.write_termail);