如何添加在警告对话框两个编辑文本字段 [英] How to add two edit text fields in an alert dialog
问题描述
我想用一个警告对话框,提示输入用户名和Android的密码。我发现这个code 这里:
I am trying to use an alert dialog to prompt for a username and a password in android. I have found this code here:
if (token.equals("Not Found"))
{
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.userpasslayout, null);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Please Login to Fogbugz");
alert.setMessage("Enter your email and password");
// Set an EditText view to get user input
alert.setView(textEntryView);
AlertDialog loginPrompt = alert.create();
final EditText input1 = (EditText) loginPrompt.findViewById(R.id.username);
final EditText input2 = (EditText) loginPrompt.findViewById(R.id.password);
alert.setPositiveButton("Login", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input1.getText().toString(); **THIS CRASHES THE APPLICATION**
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
编辑:我是能够建立适当的布局,但收到一个错误,当我尝试访问文本字段。有什么问题吗?
I was able to set up the proper layout, but receive an error when I try to access the text field. What is the problem here?
推荐答案
在 API演示在Android SDK中有不只是一个例子。
The API Demos in the Android SDK have an example that does just that.
这是在 DIALOG_TEXT_ENTRY
。他们有一个布局,夸大它 LayoutInflater
,并用它作为视图。
It's under DIALOG_TEXT_ENTRY
. They have a layout, inflate it with a LayoutInflater
, and use that as the View.
编辑:我已经在我原来的答复链接到已过时。这里是一个<一个href="http://www.linuxtopia.org/online_books/android/devguide/guide/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html"相对=nofollow>镜。
What I had linked to in my original answer is stale. Here is a mirror.
这篇关于如何添加在警告对话框两个编辑文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!