2013-03-02 10 views
3

У меня есть следующая функция, которая вызывает всплывающее окно из нажатия кнопки меню. У этого есть кнопка ok, чтобы закрыть всплывающее окно. Но функция onclick не запускается при нажатии кнопки. Кроме того, мне нужно закрыть всплывающее окно, когда нажата кнопка «Назад».Закрытие всплывающего окна на Android

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

Спасибо, я получил, где он был не прав , изменено на View 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(); 
} 
Смежные вопросы