2014-11-14 33 views
0

我開始像3天前的Android編程,我得到了PopupWindows的麻煩。我的想法是在上下文菜單中顯示關於我的應用程序的一些細節(默認按鈕稱爲設置),所以當按鈕被點擊時,必須出現Popupwindow窗口。如何popupWindow從不同的活動

我在這裏我的班,我應該從任何其他類relatid調用任何活動,以顯示彈出。

public class MostrarDetallesApp extends Activity implements View.OnClickListener { 

PopupWindow popupWindow; 

public void detallesApp(MenuItem item) { 
    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
    View popupView = layoutInflater.inflate(R.layout.detallesaplicacion, null); 
    View background = this.getCurrentFocus(); 
    popupWindow = new PopupWindow(
      popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 


    Button cerrarDetallesApp = (Button)popupView.findViewById(R.id.cerrarDetallesAppButton); 
    cerrarDetallesApp.setOnClickListener(new Button.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      popupWindow.dismiss(); 
     } 
    }); 
    popupWindow.showAtLocation(background,Gravity.CENTER,Gravity.CENTER,Gravity.CENTER); 
    popupWindow.setAnimationStyle(android.R.style.Animation_Toast); 
    popupWindow.setFocusable(true); 
    popupWindow.update(); 


} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    popupWindow.dismiss(); 
} 

} 的解僱與該popupwindow是爲了關閉按鈕相關的。現在我的問題是,如何從每個類調用該彈出窗口,以避免在每個類中都複製代碼?

我應該讓那一個靜態,但如果我這樣做,方法就像this.getCurrentFocus();無法使用。

我不是100%的whatevery語法單詞,因爲我投入了3-4小時尋找如何從該菜單彈出窗口,所以,例如,我可以告訴你MenuItem項必須在那裏調用,因爲不知何故,它可以識別用戶何時點擊按鈕。

謝謝您的幫助:)

回答

0

使用上下文參數只是創建這個類的一個對象,然後調用的ShowWindow():

在活動電話:

MostrarDetallesApp obj=new MostrarDetallesApp(this); 
obj.showWindow(); 


public class MostrarDetallesApp implements View.OnClickListener { 
PopupWindow popupWindow; 
private Context context; 

MostrarDetallesApp(Context context) { 
    this.context = context; 
} 

public void showWindow() { 
    LayoutInflater layoutInflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View popupView = layoutInflater.inflate(R.layout.detallesaplicacion, 
      null); 
    View background = this.getCurrentFocus(); 
    popupWindow = new PopupWindow(popupView, 
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 

    Button cerrarDetallesApp = (Button) popupView 
      .findViewById(R.id.cerrarDetallesAppButton); 
    cerrarDetallesApp.setOnClickListener(new Button.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      popupWindow.dismiss(); 
     } 
    }); 
    popupWindow.showAtLocation(background, Gravity.CENTER, Gravity.CENTER, 
      Gravity.CENTER); 
    popupWindow.setAnimationStyle(android.R.style.Animation_Toast); 
    popupWindow.setFocusable(true); 
    popupWindow.update(); 

} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    popupWindow.dismiss(); 
} 

}

+0

謝謝你的幫助,真的:) 我剛剛得到的問題與「this.getCurrentFocus(); – Razvi 2014-11-14 13:50:54

+0

我的問題來了,當調用popupWindow .shotAtLocation(background ... 事情是,這種方法需要一個視圖來顯示彈出窗口,但我只是不知道如何從另一個類中調用它。 – Razvi 2014-11-14 14:18:58

+0

現在我得到NullPointerExpcetion在 LayoutInflater layoutInflater =(LayoutInflater)getBaseContext()。getSystemService(LAYOUT_INFLATER_SERVICE); – Razvi 2014-11-14 14:58:00

相關問題