2015-09-25 54 views
0

你好傢伙需要一些幫助我創建了一個自定義alertdialog,它接受用戶輸入,如用戶名和密碼。我跟着the android developers site我想要做的是一旦用戶輸入用戶名和密碼,然後按alertdialog中的登錄按鈕我想要顯示這些值創建對話框的活動。我被困在這浪費了3個小時。任何幫助將非常感激。這是我的代碼。將數據發送回alertDialog的活動

MainActivity.java

public class MainActivity extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{ 

private Button dialogButton; 
private Button customDialogButton; 
private TextView customDialogTextview; 

private Button dialogWithInterface; 
private EditText dialogUsername; 
private EditText dialogPassword; 
String username; 
String password; 


public void showNoticeDialog() { 
    // Create an instance of the dialog fragment and show it 
    DialogFragment dialog = new NoticeDialogFragment(); 
    dialog.show(getFragmentManager(), "NoticeDialogFragment"); 

} 



// The dialog fragment receives a reference to this Activity through the 
// Fragment.onAttach() callback, which it uses to call the following methods 
// defined by the NoticeDialogFragment.NoticeDialogListener interface 
@Override 
public void onDialogPositiveClick(DialogFragment dialog) { 
    // User touched the dialog's positive button 

} 

@Override 
public void onDialogNegativeClick(DialogFragment dialog) { 
    // User touched the dialog's negative button 

} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    customDialogTextview = (TextView)findViewById(R.id.customdialogtext); 
    customDialogTextview.setText("Email and Password: "); 

    dialogButton = (Button)findViewById(R.id.dialogbutton); 
    dialogButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // 1. Instantiate an AlertDialog.Builder with its constructor 
      final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
      // Add the buttons 
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // User clicked OK button 

       } 
      }); 

      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // User cancelled the dialog 
       } 
      }); 

      // 2. Chain together various setter methods to set the dialog characteristics 
      builder.setMessage("hello") 
        .setTitle("Dialog"); 

      // 3. Get the AlertDialog from create() 
      AlertDialog dialog = builder.create(); 
      dialog.show(); 
     } 
    }); 

    customDialogButton = (Button)findViewById(R.id.customdialogbutton); 
    customDialogButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
      // Get the layout inflater 
      LayoutInflater inflater = MainActivity.this.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.dialog, null)); 

      builder.setPositiveButton("Sign In", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        //sign in the user 
       } 
      }); 

      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 

      AlertDialog dialog = builder.create(); 
      dialog.show(); 

     } 
    }); 

    dialogWithInterface = (Button)findViewById(R.id.dialogwithinterface); 
    dialogWithInterface.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      showNoticeDialog(); 
     } 
    }); 

} 

NoticeDialogFragment.java

public class NoticeDialogFragment extends DialogFragment { 


/* The activity that creates an instance of this dialog fragment must 
* implement this interface in order to receive event callbacks. 
* Each method passes the DialogFragment in case the host needs to query it. */ 
public interface NoticeDialogListener { 
    public void onDialogPositiveClick(DialogFragment dialog); 
    public void onDialogNegativeClick(DialogFragment dialog); 

} 

// Use this instance of the interface to deliver action events 
NoticeDialogListener mListener; 

// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener 
@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    // Verify that the host activity implements the callback interface 
    try { 
     // Instantiate the NoticeDialogListener so we can send events to the host 
     mListener = (NoticeDialogListener) activity; 
    } catch (ClassCastException e) { 
     // The activity doesn't implement the interface, throw exception 
     throw new ClassCastException(activity.toString() 
       + " must implement NoticeDialogListener"); 
    } 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // Build the dialog and set up the button click handlers 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    builder.setView(inflater.inflate(R.layout.dialog, null)); 
    builder.setPositiveButton("Sign In", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      // Send the positive button event back to the host activity 
      mListener.onDialogPositiveClick(NoticeDialogFragment.this); 
      Toast.makeText(getActivity(), "positive", Toast.LENGTH_SHORT).show(); 


     } 
    }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // Send the negative button event back to the host activity 
        mListener.onDialogNegativeClick(NoticeDialogFragment.this); 
       } 
      }); 
    return builder.create(); 
} 

} 

回答

1

看看這個方法您的活動實現:

@Override 
public void onDialogPositiveClick(DialogFragment dialog) { 
    // User touched the dialog's positive button 
} 

它是除了您在創建自定義界面名爲NoticeDialogListener的對話框,並且是您希望對話與該行爲進行通信的方式這種稱呼。

public interface NoticeDialogListener { 
    public void onDialogPositiveClick(DialogFragment dialog); 
    public void onDialogNegativeClick(DialogFragment dialog); 
} 

改變這種做法,onDialogPostiveClick看起來類似:

public void onDialogPositiveClick(String name, String password); 

當按鈕被點擊,像這樣從你的EditText值傳遞到呼叫:

builder.setPositiveButton("Sign In", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     // Send the positive button event back to the host activity 
     mListener.onDialogPositiveClick(mNameEdit.getText().toString(), mPasswordEdit.getText().toString()); 
    } 
}); 

下步驟是做任何你想做的事情,你的方法中的名字和密碼值在onDialogPositiveClick()方法的活動中被覆蓋。

@Override 
public void onDialogPositiveClick(String name, String password) { 
    //do something with name and password here? 
} 

這似乎是最簡單的方法來做你想用你已有的代碼。

+0

已經解決了。我在界面中製作了另一種方法,在我的主要活動中使用了2個字符串並覆蓋了該方法,並且您的答案同樣非常感謝您的幫助。 – AIK

相關問題