1

我試圖做一個連接彈出像男人: http://developer.android.com/guide/topics/ui/dialogs.html如何恢復PARAM在DialogFrament像彈出

像人的爲例做,但現在我不能收回的登錄名和密碼由用戶填充。

佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top|center" 
     android:contentDescription="@string/logo" 
     android:scaleType="center" 
     android:src="@drawable/logo" /> 

    <EditText 
     android:id="@+id/edtlogin" 
     android:inputType="textEmailAddress" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginBottom="4dp" 
     android:hint="@string/identifiant" /> 
    <EditText 
     android:id="@+id/edtpassword" 
     android:inputType="textPassword" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="4dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginBottom="16dp" 
     android:fontFamily="sans-serif" 
     android:hint="@string/motdepasse"/> 
</LinearLayout> 

public class PopUpConnexion extends DialogFragment { 

    private EditText login; 


    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity()); 
     // Get the layout inflater 
     LayoutInflater inflater = this.getActivity().getLayoutInflater(); 

     // Inflate and set the layout for the dialog 
     // Pass null as the parent view because its going in the dialog layout 


     builder.setView(inflater.inflate(R.layout.popup_connexion, null)) 
     // Add action buttons 
       .setPositiveButton(R.string.connexion, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 


// Here I want to recover the login and the password for sign in. 


        } 
       }) 
       .setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         PopUpConnexion.this.getDialog().cancel(); 
        } 
       });  
     return builder.create(); 
    } 

} 

我試圖用一切手段,但類型的EditText的對象登錄總是空收回值。 由於

編輯:

public class PopUpConnexion extends DialogFragment { 

    EditText login; 
    EditText password; 


    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity()); 
     // Get the layout inflater 
     LayoutInflater inflater = this.getActivity().getLayoutInflater(); 

     View textEntryView = inflater.inflate(R.layout.popup_connexion, null); 
     login = (EditText) textEntryView.findViewById(R.id.edtlogin); // with the debugger we can see it's the good EditText with the id 
     login.setText("test"); // we can't see "test" in the EditText 


     builder.setView(inflater.inflate(R.layout.popup_connexion, null)) 
     // Add action buttons 
       .setPositiveButton(R.string.connexion, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 

          String tmp = "login: " + login.getText().toString() + "|"; // with the debugger we can see it's the good EditText with the id 
          Toast.makeText(getActivity().getApplication().getApplicationContext(), tmp, Toast.LENGTH_SHORT).show(); 
        } 
       }) 
       .setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         PopUpConnexion.this.getDialog().cancel(); 
        } 
       });  
     return builder.create(); 
    } 

} 

隨着login.setText( 「測試」);我們可以得出這樣的結論:可以看到的視圖與我getText()的視圖不一樣。

最後編輯:

public class PopUpConnexion extends DialogFragment { 

    EditText identifiant; 
    EditText motdepasse; 
    Button Connexion; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity()); 
     // Get the layout inflater 
     LayoutInflater inflater = this.getActivity().getLayoutInflater(); 

     View textEntryView = inflater.inflate(R.layout.popup_connexion, null); 
     login = (EditText) textEntryView.findViewById(R.id.edtlogin); 
     password = (EditText) textEntryView.findViewById(R.id.edtpassword); 
     Connexion = (Button) textEntryView.findViewById(R.id.loginButton); 


     Connexion.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(getActivity().getApplication().getApplicationContext(), login.getText() + " " + password.getText(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     builder.setView(textEntryView); 
     return builder.create(); 
    } 

} 

最後我不使用事件管理器像男人。 我已經測試過這個技術,但是當你剛剛編寫OnClickListener時,由於DialogFragment.OnClickListener和View.OnClickListener的困惑,我一直有些錯誤。 現在,它的工作,但我認爲它存在一個更好的方法使用builder.setPositiveButon()& co。

回答

-1

你想顯示對話框frnd。你想要的那種類型

+0

我不明白什麼是「frnd」,但我想做一些鏈接:http://developer.android.com/images/ui/dialog_custom.png – Marchah

+0

好吧,你只需要顯示彈出窗口手段使用Dialog videoDialog = new Dialog(YouActivityname.this); \t \t \t \t \t \t videoDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); \t \t \t \t \t \t videoDialog.setContentView(R.layout.opecofabpopup); \t \t \t \t \t \t videoDialog.setCanceledOnTouchOutside(false); \t \t \t \t \t \t videoDialog.show(); – saravanan

+0

我已經到達顯示彈出窗口,我只想恢復彈出窗口中EditText的值。我編輯第一個msg用於顯示我想要的數據。 – Marchah