繁体   English   中英

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

[英]In Android, how do I create a popup and submit data to the main view?

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

目前,我可以按按钮,然后会出现弹出窗口,但是我不知道如何将用户输入数据提交到主视图,或者仅通过按按钮返回到主视图。

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 (...) {

        }
    }
}

我看到您正在使用PopupWindow-调用dismiss()将其删除。

如果您只希望弹出窗口捕获一些用户输入,然后返回产生弹出窗口的活动,则建议使用“自定义对话框”。 您可以在对话框中创建所需的任何内容,并在每个按钮的处理程序中添加所需的任何按钮。 一个例子;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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