简体   繁体   中英

how to dismiss a pop up window?

private void newGame() {


        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final PopupWindow pw = new PopupWindow(inflater.inflate(
                R.layout.settings, null, true), 300, 600, true);

        pw.showAtLocation(this.findViewById(R.id.settings), Gravity.RIGHT, 0, 0);

        pw.setTouchInterceptor(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // your code when touched on the event


                return false;
            }

        });
    }

I have posted my code here.I am getting the pop up window.but i dont know how to add events to that pop up window and make it invisible after clicking it.

how to handle the Popup window and alert - just follow this code -

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;`enter code here`
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Alart_Pop_Handel {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        Set<String> wind = driver.getWindowHandles();
        System.out.println("total window size -> " +wind.size()); 
        // blank fire fox will open 

        Iterator<String> it = wind.iterator();
        System.out.println(it.next());  // show the window id 
        driver.get("http://in.rediff.com/");

        wind = driver.getWindowHandles(); // call one more time to get window
        it = wind.iterator();
        System.out.println(" total window size -> " +wind.size());
         // show 2 window and id 

        // handel pop up 
        // create string 

        String main= it.next();
        String pop= it.next();

        System.out.println(main); // take a print for both window id 
        System.out.println(pop);

        driver.switchTo().window(pop); // control change to pop up 
        driver.close();                 // close pop up 
        driver.switchTo().window(main);  // get back to main window

        System.out.println("************ main window id*****************");
        System.out.println(main); // make sure the main window id will see

        //click signin 

        driver.findElement(By.xpath(".//*[@id='signin_info']/a[1]")).click();
        driver.findElement(By.xpath("//input[@type='submit']")).click();

        // handel alert
        Alert al=driver.switchTo().alert();
        System.out.println(al.getText());
        al.accept();
        driver.switchTo().defaultContent();

Call the method dismiss in the method onTouch

    public boolean onTouch(View v, MotionEvent event) { 
        // your code when touched on the event 
        // you can add events here when the pop up window is touched
        pw.dismiss();
        return true; 
    } 

Is it some kind of custom Alert View, meaning some kind of text, textinput, ... and one or more buttons ("OK", "Cancel")?

Then you could use the following code:

protected Dialog onCreateDialog(int id) {
    Dialog dialog = new Dialog(this);
    switch (id) {
    case DIALOG_HELP_ID:
        // This example shows how to add a custom layout to an AlertDialog
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(
                R.layout.helpdialog_main, null);
        return new AlertDialog.Builder(Main.this)
                .setView(textEntryView)
                .setPositiveButton(R.string.stringHelptextButtonOK,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {

                                // Popup is closed automatically, nothing
                                // needs to be done here
                            }
                        }).create();

    default:
            dialog = null;
        }
        return dialog;
    }
public class Pop extends Activity {
    PopupWindow pw;

    /** Called when the activity is first created. */
    Button ok;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ok = (Button) findViewById(R.id.but);
        ok.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {

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

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

                pw.showAtLocation(findViewById(R.id.mainn), Gravity.BOTTOM, 0,0);
            }
        });
    }

    public void onbuttonClick(View v) {

        Intent openstartingpoint2=new Intent("com.lee.pop.CLA");
        startActivity(openstartingpoint2);

        pw.dismiss();
        return ;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM