Android how to create popup window

2019-03-30 11:08发布

问题:

I need to create a popup window with buttons and a button that will close the popup. I found some tutorials but I couldn't find out how to do the implementation.

What I want to do: Click an action button and the popup shows and when I click the the close button the popup window must close.

There were an onCreate method in the tuorials and I didn't understand how is it called.

Can somebody give an example of an popup implementation or a link to a good tutorial? Thank you!

回答1:

private void showPopup(){
    Button btn_closepopup=(Button)layout.findViewById(R.id.btn_closePoppup);
    pwindo=new PopupWindow(layout,480,500,true);
    pwindo.showAtLocation(layout, Gravity.CENTER, 0, 40);
    chartContainer1.addView(mChart);
    btn_closepopup.setOnClickListener(new OnClickListener() {

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


回答2:

 private void callPopup() {

 LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
 .getSystemService(LAYOUT_INFLATER_SERVICE);

View popupView = layoutInflater.inflate(R.layout.popup, null);

 popupWindow=new PopupWindow(popupView,
         LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,        
 true);

 popupWindow.setTouchable(true);
 popupWindow.setFocusable(true);

 popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
 Name = (EditText) popupView.findViewById(R.id.edtimageName);

((Button) popupView.findViewById(R.id.saveBtn))
 .setOnClickListener(new OnClickListener() {

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
 public void onClick(View arg0) {
 Toast.makeText(getApplicationContext(),
     Name.getText().toString(),Toast.LENGTH_LONG).show();

  popupWindow.dismiss();

 }

});

((Button) popupView.findViewById(R.id.cancelbtutton))
  .setOnClickListener(new OnClickListener() {

  public void onClick(View arg0) {

   popupWindow.dismiss();
  }
});