如何解雇弹出窗口?

时间:2011-09-22 06:36:28

标签: android

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

        });
    }

我在这里发布了我的代码。我正在弹出窗口。但是我不知道如何在弹出窗口中添加事件并在点击后使其不可见。

4 个答案:

答案 0 :(得分:0)

调用方法dismiss

中的方法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; 
    } 

答案 1 :(得分:0)

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

然后您可以使用以下代码:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如何处理弹出窗口和警报 - 只需按照此代码 -

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();