2013-03-02 2 views
3

나는 메뉴 버튼 클릭으로부터 팝업을 호출하는 다음과 같은 기능을 가지고있다. 팝업 창을 닫으려면 ok 버튼이 있습니다. 그러나 onclick 기능은 단추 누르기에서 시작되지 않습니다. 또한 뒤로 버튼을 누르면 팝업을 닫아야합니다.안드로이드에서 팝업 닫기

LayoutInflater inflater = (LayoutInflater) MainActivity.this 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.about_popup, null, false),400,440, true); 

    pw.showAtLocation(lv, Gravity.CENTER, 0, 0); 
    View popupView=inflater.inflate(R.layout.about_popup, null, false); 
    Button close = (Button) popupView.findViewById(R.id.okbutton); 
    close.setOnClickListener(new OnClickListener() { 

     public void onClick(View popupView) { 
     pw.dismiss(); 
     } 
    }); 

감사

답변

0

변경이

View popupView=inflater.inflate(R.layout.about_popup, null, false); 
    Button close = (Button) popupView.findViewById(R.id.okbutton); 

현재
Button close = (Button) pw.findViewById(R.id.okbutton); 
7

에 당신은 PopupWindow에보기의 다른 인스턴스를 전달하고 차이 인스턴스의 버튼 같은 인스턴스를 사용 발견하려고 노력하고 있습니다 어떤 단추를 찾으려면 PopupWindow에 전달했습니다.

LayoutInflater inflater = (LayoutInflater) MainActivity.this 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View popupView = layoutInflater.inflate(R.layout.about_popup, null, false); 
    final PopupWindow pw = new PopupWindow(popupView,400,440, true); 

    pw.showAtLocation(lv, Gravity.CENTER, 0, 0); 
    Button close = (Button) popupView.findViewById(R.id.okbutton); 
    close.setOnClickListener(new OnClickListener() { 

     public void onClick(View popupView) { 
     pw.dismiss(); 
     } 
    }); 

초 방법으로 버튼을 다시 레이아웃을 팽창의 내부 현재 창에 버튼을 찾을 수 PopupWindow 인스턴스를 사용할 수 있습니다 : : 귀하의 코드를 변경 잘못 어디에

Button close = (Button) pw.findViewById(R.id.okbutton); 
close.setOnClickListener(new OnClickListener() { 

      public void onClick(View popupView) { 
      pw.dismiss(); 
      } 
     }); 
+1

감사합니다, 나는 얻었다 . 이보기로 변경되었습니다. popupView = inflater.inflate (R.layout.about_popup, null, false); – Arun

1
CustomDialogClass cdd = new CustomDialogClass(this, 
       "Internet Connection Error", 
       "Sorry, no internet connection.Feeds cannot be refreshed", 
       "Alert"); 
     cdd.show(); 

public class CustomDialogClass extends Dialog implements 
    android.view.View.OnClickListener { 
String txtTitle, txtText, txtType; 
Button btn; 
TextView alertText; 
ImageView imgVw; 

public CustomDialogClass(Context context, String title, String text, 
     String type) { 
    super(context); 
    txtTitle = title; 
    txtText = text; 

} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.alert_layout); 
    imgVw = (ImageView) findViewById(R.id.iconImgview); 

    alertText = (TextView) findViewById(R.id.AlertText); 
    alertText.setText(txtText); 

    btn = (Button) findViewById(R.id.dismis_dialog); 
    btn.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    dismiss(); 
}