2012-11-04 40 views
6
//create inflater 
final LayoutInflater inflater = (LayoutInflater) this 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
//create popupwindow 
    PopupWindow pw=new PopupWindow(inflater.inflate(R.layout.menu, (ViewGroup)findViewById(R.layout.dictionarylist))); 

     Button Menu = (Button) findViewById(R.id.Menu); 
     Menu.setOnClickListener(new Button.OnClickListener() { 
      public void onClick(View v) { 
       pw.showAtLocation(v, Gravity.CENTER, 0, 0); 
       pw.update(0, 0, 200, 250); 
       pw.setOutsideTouchable(false); 
      } 
     }); 

我想要的是顯示彈出窗口,當我點擊父活動中的按鈕。彈出窗口有按鈕,當點擊按鈕它做一些功能。如何從彈出窗口獲得輸入

enter image description here

回答

1

你必須找到按鈕的視圖,然後監聽分配給它這樣的:

View pview=inflater.inflate(R.layout.menu, (ViewGroup)findViewById(R.layout.dictionarylist)); 

Button Menu = (Button) pview.findViewById(R.id.Menu); 

Menu.setOnClickListener(new Button.OnClickListener() { 
      public void onClick(View v) { 
       pw.showAtLocation(v, Gravity.CENTER, 0, 0); 
       pw.update(0, 0, 200, 250); 
       pw.setOutsideTouchable(false); 
      } 

而且初始化充氣,如果你還沒有這樣的:

Inflator inflator = LayoutInflater.from(this);