如何在警报对话框中添加两个编辑文本字段 [英] How to add two edit text fields in an alert dialog

查看:140
本文介绍了如何在警报对话框中添加两个编辑文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用警报对话框在android中提示输入用户名和密码。我发现这个代码 here:

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?

推荐答案

Android SDK中的 API演示有一个例子,

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.

编辑:我已经链接到我原来的答案是陈旧的。这是一个镜像

What I had linked to in my original answer is stale. Here is a mirror.

这篇关于如何在警报对话框中添加两个编辑文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆