我想在android中使用警告对话框提示输入用户名和密码。我发现这个代码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();
}
编辑:我是能够建立适当的布局,但是当我尝试访问该文本字段收到错误信息。这里有什么问题?
这个工作,但是当我尝试访问的EditText我仍然收到一个错误。我编辑了我的答案,是否有一些问题,因为这些字段尚未构建? – mbauer14 2010-08-06 21:13:23
什么是“错误”? – EboMike 2010-12-05 02:19:34
此网站上不再有此示例代码或最新样品 – conners 2012-07-18 08:42:21