2010-08-06 113 views
52

我想在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(); 

    } 

編輯:我是能夠建立適當的佈局,但是當我嘗試訪問該文本字段收到錯誤信息。這裏有什麼問題?

回答

25

在Android SDK中的API Demos有一個例子。

它在DIALOG_TEXT_ENTRY之下。他們有一個佈局,用LayoutInflater誇大它,並用它作爲視圖。

編輯:我在原來的答案中鏈接到的是陳舊的。這是一個mirror

+0

這個工作,但是當我嘗試訪問的EditText我仍然收到一個錯誤。我編輯了我的答案,是否有一些問題,因爲這些字段尚未構建? – mbauer14 2010-08-06 21:13:23

+3

什麼是「錯誤」? – EboMike 2010-12-05 02:19:34

+0

此網站上不再有此示例代碼或最新樣品 – conners 2012-07-18 08:42:21

1

看看AlertDialog docs。由於它指出,到一個自定義視圖添加到您的警告對話框,你需要找到的FrameLayout,並添加您的視圖,像這樣:

FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom); 
fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT)); 

最有可能你會希望創造一個佈局xml文件的查看並膨脹它:

LayoutInflater inflater = getLayoutInflater(); 
View twoEdits = inflater.inflate(R.layout.my_layout, f1, false); 
19

在代碼中使用這些行,因爲textEntryView是用戶名edittext和密碼edittext的父級。

final EditText input1 = (EditText) textEntryView .findViewById(R.id.username); 
    final EditText input2 = (EditText) textEntryView .findViewById(R.id.password); 
+0

已經停留了這麼久。我以爲我沒有正確充氣,但事實證明我使用了錯誤的父母。感謝您的輸入南迪! – Kitteh 2011-09-21 09:20:16

62

檢查此警告框中的代碼有編輯textview時單擊確定它使用吐司顯示在屏幕上。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    final EditText input = new EditText(this); 
    alert.setView(input); 
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      String value = input.getText().toString().trim(); 
      Toast.makeText(getApplicationContext(), value, 
       Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      dialog.cancel(); 
     } 
    }); 
    alert.show();    
} 
+15

他想要兩個EditText字段..不是一個 – Dediqated 2013-09-13 08:33:17

+0

只需添加另一個名爲input2,然後alert.setView(input2); – Nepster 2014-05-08 06:05:58

+6

@Nepster - 'set'表示新視圖(input2)將覆蓋舊視圖(input1)。 – 2014-06-09 11:58:26

0

我發現了另外一組例子來自一個名叫Mossila的男人定製AlertDialog。我認爲他們比谷歌的例子更好。要快速查看Google的API演示,您必須將他們的演示jar導入到您的項目中,這可能不需要。

但是Mossila的示例代碼是完全獨立的。它可以直接剪切並粘貼到您的項目中。它只是工作!那麼你只需要調整它以滿足你的需求。請參閱here

10
LayoutInflater factory = LayoutInflater.from(this); 
final View textEntryView = factory.inflate(R.layout.text_entry, null); 
//text_entry is an Layout XML file containing two text field to display in alert dialog 
final EditText input1 = (EditText) textEntryView.findViewById(R.id.EditText1); 
final EditText input2 = (EditText) textEntryView.findViewById(R.id.EditText2);    
input1.setText("DefaultValue", TextView.BufferType.EDITABLE); 
input2.setText("DefaultValue", TextView.BufferType.EDITABLE); 
final AlertDialog.Builder alert = new AlertDialog.Builder(this); 

alert.setIcon(R.drawable.icon) 
    .setTitle("Enter the Text:") 
    .setView(textEntryView) 
    .setPositiveButton("Save", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
        Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString()); 
        Log.i("AlertDialog","TextEntry 2 Entered "+input2.getText().toString()); 
        /* User clicked OK so do some stuff */ 
      } 
     }) 
    .setNegativeButton("Cancel", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, 
        int whichButton) { 
      } 
     }); 
alert.show(); 
2

請檢查以下代碼。它以編程方式顯示了2個編輯文本字段,沒有任何佈局xml。如果您在片段中使用它,請將'this'更改爲'getActivity()'。

棘手的是,我們必須在創建警報對話框後設置第二個文本字段的輸入類型,否則第二個文本字段將顯示文本而不是點。

public void showInput() { 
     OnFocusChangeListener onFocusChangeListener = new OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(final View v, boolean hasFocus) { 
       if (hasFocus) { 
        // Must use message queue to show keyboard 
        v.post(new Runnable() { 
         @Override 
         public void run() { 
          InputMethodManager inputMethodManager= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
          inputMethodManager.showSoftInput(v, 0); 
         } 
        }); 
       } 
      } 
     }; 

     final EditText editTextName = new EditText(this); 
     editTextName.setHint("Name"); 
     editTextName.setFocusable(true); 
     editTextName.setClickable(true); 
     editTextName.setFocusableInTouchMode(true); 
     editTextName.setSelectAllOnFocus(true); 
     editTextName.setSingleLine(true); 
     editTextName.setImeOptions(EditorInfo.IME_ACTION_NEXT); 
     editTextName.setOnFocusChangeListener(onFocusChangeListener); 

     final EditText editTextPassword = new EditText(this); 
     editTextPassword.setHint("Password"); 
     editTextPassword.setFocusable(true); 
     editTextPassword.setClickable(true); 
     editTextPassword.setFocusableInTouchMode(true); 
     editTextPassword.setSelectAllOnFocus(true); 
     editTextPassword.setSingleLine(true); 
     editTextPassword.setImeOptions(EditorInfo.IME_ACTION_DONE); 
     editTextPassword.setOnFocusChangeListener(onFocusChangeListener); 

     LinearLayout linearLayout = new LinearLayout(this); 
     linearLayout.setOrientation(LinearLayout.VERTICAL); 
     linearLayout.addView(editTextName); 
     linearLayout.addView(editTextPassword); 

     DialogInterface.OnClickListener alertDialogClickListener = new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       switch (which){ 
       case DialogInterface.BUTTON_POSITIVE: 
        // Done button clicked 
        break; 
       case DialogInterface.BUTTON_NEGATIVE: 
        // Cancel button clicked 
        break; 
       } 
      } 
     }; 
     final AlertDialog alertDialog = (new AlertDialog.Builder(this)).setMessage("Please enter name and password") 
       .setView(linearLayout) 
       .setPositiveButton("Done", alertDialogClickListener) 
       .setNegativeButton("Cancel", alertDialogClickListener) 
       .create(); 

     editTextName.setOnEditorActionListener(new OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       editTextPassword.requestFocus(); // Press Return to focus next one 
       return false; 
      } 
     }); 
     editTextPassword.setOnEditorActionListener(new OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       // Press Return to invoke positive button on alertDialog. 
       alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); 
       return false; 
      } 
     }); 

     // Must set password mode after creating alert dialog. 
     editTextPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 
     editTextPassword.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
     alertDialog.show(); 
    } 
7
   /* Didn't test it but this should work "out of the box" */ 

       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       //you should edit this to fit your needs 
       builder.setTitle("Double Edit Text"); 

       final EditText one = new EditText(this); 
       from.setHint("one");//optional 
       final EditText two = new EditText(this); 
       to.setHint("two");//optional 

       //in my example i use TYPE_CLASS_NUMBER for input only numbers 
       from.setInputType(InputType.TYPE_CLASS_NUMBER); 
       to.setInputType(InputType.TYPE_CLASS_NUMBER); 

       LinearLayout lay = new LinearLayout(this); 
       lay.setOrientation(LinearLayout.VERTICAL); 
       lay.addView(one); 
       lay.addView(two); 
       builder.setView(lay); 

       // Set up the buttons 
       builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         //get the two inputs 
         int i = Integer.parseInt(one.getText().toString()); 
         int j = Integer.parseInt(two.getText().toString()); 
        } 
       }); 

       builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         dialog.cancel(); 
       } 
       }); 
       builder.show(); 
相關問題