Android 如何关闭弹出窗口?

Android 如何关闭弹出窗口?,android,Android,我已在此处发布了我的代码。我正在获取弹出窗口。但我不知道如何将事件添加到该弹出窗口,并在单击后使其不可见。在方法onTouch中调用方法discover private void newGame() { LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final PopupWin

我已在此处发布了我的代码。我正在获取弹出窗口。但我不知道如何将事件添加到该弹出窗口,并在单击后使其不可见。

在方法
onTouch中调用方法
discover

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;
            }

        });
    }

它是某种自定义警报视图,意味着某种文本、文本输入。。。以及一个或多个按钮(“确定”、“取消”)

然后可以使用以下代码:

    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; 
    } 

如何处理弹出窗口和警报-只需遵循以下代码-

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 ;
    }
}
import java.util.Iterator;
导入java.util.Set;
导入org.openqa.selenium.Alert;
导入org.openqa.selenium.By`在这里输入代码`
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.firefox.FirefoxDriver;
公共级阿勒特·波普·汉德尔{
公共静态void main(字符串[]args){
WebDriver=newfirefoxdriver();
设置wind=driver.getWindowHandles();
System.out.println(“总窗口大小->”+wind.size());
//空白火狐将打开
迭代器it=wind.Iterator();
System.out.println(it.next());//显示窗口id
驱动程序。获取(“http://in.rediff.com/");
wind=driver.getWindowHandles();//再次调用以获取窗口
it=wind.iterator();
System.out.println(“总窗口大小->”+wind.size());
//显示2个窗口和id
//汉德尔弹出
//创建字符串
String main=it.next();
字符串pop=it.next();
System.out.println(main);//打印两个窗口id
系统输出打印项次(pop);
driver.switchTo().window(pop);//控件更改为弹出
driver.close();//关闭弹出窗口
driver.switchTo().window(主);//返回主窗口
System.out.println(“*************主窗口id**********************”);
System.out.println(main);//确保主窗口id将看到
//单击登录
driver.findElement(By.xpath(“./*[@id='signin_info']]/a[1]”)。单击();
findElement(By.xpath(//input[@type='submit']))。单击();
//汉德尔警报
Alert al=驱动程序。切换到()。Alert();
System.out.println(al.getText());
al.accept();
driver.switchTo().defaultContent();

我没有收到错误。它正在运行,但弹出窗口没有被清除。在调用
清除()
方法之前,您需要调用此方法
showAsDropDown()
,否则它不会有任何效果。这里的更多信息有助于了解如何执行此操作。
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 ;
    }
}
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();