首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Android中,如何创建弹出窗口并将数据提交到主视图?

在Android中,如何创建弹出窗口并将数据提交到主视图?
EN

Stack Overflow用户
提问于 2011-11-23 02:40:01
回答 1查看 4.8K关注 0票数 0

我已经创建了一个带有按钮的Main.xml。它们都执行特定的操作,这一切都很好,但也应该有密码保护的按钮。因此,我还创建了第二个xml (popup.xml)。如果用户按下该按钮,则会弹出此消息。在popup.xml中,只有一个用于用户输入的文本字段和一个用于提交的按钮。

此时,我可以按下按钮,弹出窗口就会出现,但我不知道如何将用户输入的数据提交到主视图,或者只是通过按下按钮返回到主视图。

代码语言:javascript
运行
复制
public class BastiLauncherActivity extends Activity implements OnClickListener {

    private Button b1;    
    // ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // this b1 is a button in the main view where this pop up should appear
        b1 = (Button) findViewById(R.id.b1Button);
        b1.setOnClickListener(this);
        // ...
    }

    @Override
    public void onClick(View v) {
        LayoutInflater inflater =
                (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup, null,
                false), 200, 300, true);
        pw.setOutsideTouchable(true);

        if (v == b1) {
            // opening the popup
            pw.showAtLocation(findViewById(R.id.dateiButton), Gravity.CENTER, 0, 0);

        } else if (...) {

        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-23 03:29:08

我看到您正在使用PopupWindow -要删除它,请调用dismiss()

如果你只是想要一个弹出窗口来捕获一些用户输入,然后返回到产生弹出窗口的活动,那么我建议使用一个自定义对话框。您可以在对话框中创建您喜欢的任何内容,并使用每个按钮的处理程序添加您需要的任何按钮。一个例子;

代码语言:javascript
运行
复制
new AlertDialog.Builder(Main.this)
               .setTitle("Enter password")
               .setMessage("Password required for this function")
               .setView(/* You view layout */)
               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int whichButton) {
                       Editable value = input.getText(); 
                   }
               }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int whichButton) {
                       // Do nothing.
                   }
               }).show();
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8232012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档