Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android之自定義AlertDialog無法監聽控件

Android之自定義AlertDialog無法監聽控件

編輯:關於Android編程

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

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved