要做一個自訂的彈出框,以前都是用一個Activity來實現,總覺得不是很好看,彈出的框有時候感覺有點大,所以上網查資料說,可以給AlertDialog自訂一個view就行了,代碼如下
SPAN style="FONT-SIZE: 18px">AlertDialog dialog = new AlertDialog.Builder( XXXXActivity.this).create(); dialog.show();//該代碼一定要放在給dialog設定自訂view的前面 Window window = dialog.getWindow(); window.setContentView(R.layout.termail_handle_dialog);</SPAN> AlertDialog dialog = new AlertDialog.Builder( XXXXActivity.this).create(); dialog.show();//該代碼一定要放在給dialog設定自訂view的前面 Window window = dialog.getWindow(); window.setContentView(R.layout.termail_handle_dialog);
SPAN style="FONT-SIZE: 18px"> </SPAN>
這樣可以完全展示出你所定義的view,但是該view中我添加了兩個ImageView,需要對ImageView設定監聽器,我是用下面方法做的,代碼如下
SPAN style="FONT-SIZE: 18px"> LayoutInflater factory = LayoutInflater.from(TermailFetchActivity.this); final View view = factory.inflate(R.layout.termail_handle_dialog, null);</SPAN> LayoutInflater factory = LayoutInflater.from(TermailFetchActivity.this); final View view = factory.inflate(R.layout.termail_handle_dialog, null);[java] view plaincopyprint?<SPAN style="FONT-SIZE: 18px"> ImageView scanAddTermail = (ImageView) view.findViewById(R.id.scan_termail); </SPAN> ImageView scanAddTermail = (ImageView) view.findViewById(R.id.scan_termail); [java] view plaincopyprint?<SPAN style="FONT-SIZE: 18px"> 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(); } }); </SPAN> 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(); } });
怎麼都監聽不到該控制項。後來發現網上用下面方法可以監聽到。代碼如下
SPAN style="FONT-SIZE: 18px"> </SPAN><PRE class=java name="code"><SPAN style="FONT-SIZE: 18px"> 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);</SPAN></PRE><PRE class=java name="code"> </PRE> <PRE></PRE> <PRE></PRE>