Android之自訂AlertDialog無法監聽控制項

來源:互聯網
上載者:User

          要做一個自訂的彈出框,以前都是用一個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> 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.