我已经创建了一个带有按钮的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 (...) {
}
}
}发布于 2011-11-23 03:29:08
我看到您正在使用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();https://stackoverflow.com/questions/8232012
复制相似问题