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

B77*_*770 0 java navigation android popup

我创建了一个带有按钮的 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 (...) {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

C0d*_*ack 5

我看到您正在使用 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();
Run Code Online (Sandbox Code Playgroud)