请,我想在用户必须输入某些内容后,返回 Android studio 中的警报对话框,然后显示详细信息。我使用下面的代码:
editText = (EditText) findViewById(R.id.my_edit_txt);
editText.getText().toString();
但它没有显示在我创建的确认对话框中。
最佳答案
看起来您没有设置 AlertDialog 的文本,但这只是一个假设,因为您的问题中没有足够的代码。调用 editText.getText().toString()
不会执行任何操作,只会返回一个字符串。它不会将其分配给任何东西。 AlertDialog 的示例如下:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(editText.getText().toString());
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
...
// Create the AlertDialog
AlertDialog dialog = builder.create();
我从 Android Developers 中获取了这个示例并对其进行修改,使其包含您的 EditText 的文本。此代码应该可以工作,因为您不仅调用 toString()
方法,还将其返回值分配给 AlertDialog 的消息属性。
关于java - 如何在警报对话框上显示用户输入详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48730369/