2014-07-26 65 views
1

我有一個活動,其中包括一些EditText的自我創建的登錄系統,我也包括一個Facebook登錄按鈕。當我從活動中註釋掉下面的FB代碼段時,我可以檢索EditTexts的值。但是,使用FB代碼,所有EditText值都會變回空。爲什麼是這樣?Android在同一活動中使用Facebook登錄和EditText?

public class LoginActivity extends FragmentActivity 
{ 
    private EditText mEmailRegisterView; 
    private EditText mPasswordRegisterView; 
    private EditText mPasswordConfirmRegisterView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 

    // Set FB fragment 
    // Commenting this if/else allows the EditText values to be obtained. 
    if (savedInstanceState == null) 
    { 
     // Add the fragment on initial activity setup 
     loginFragment = new LoginFragment(); 
     getSupportFragmentManager().beginTransaction() 
     .add(android.R.id.content, loginFragment).commit(); 
    } 
    else 
    { 
     // Or set the fragment from restored state info 
     loginFragment = (LoginFragment) getSupportFragmentManager().findFragmentById(
     android.R.id.content); 
    } 
    ... 
    // Get the register EditText objects 
    mEmailRegisterView = (EditText) findViewById(R.id.login_activity_email_register); 
    mPasswordRegisterView = (EditText) findViewById(R.id.login_activity_password_register); 
    mPasswordConfirmRegisterView = (EditText) findViewById(R.id.login_activity_password_confirm_register); 

    Button registerButton = (Button) findViewById(R.id.register_button); 
    registerButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) 
     { 
     // Store values at the time of the login attempt. 
     mEmailRegister = mEmailRegisterView.getText().toString(); 
     mPasswordRegister = mPasswordRegisterView.getText().toString(); 
     mPasswordConfirmRegister = mPasswordConfirmRegisterView.getText().toString(); 

     // These are all empty 
     Log.d(TAG, "Email : " + mEmailRegister); 
     Log.d(TAG, "Password : " + mPasswordRegister); 
     Log.d(TAG, "Confirm : " + mPasswordConfirmRegister); 
     } 
    }); 
    } 
} 

如果您需要更多的代碼,請告訴我。

回答

0

那是因爲你登錄嘗試時的存儲值。和存儲值之後,再次分配您的EditText在onClick method.So在onCreate方法只有一次初始化editext代替registerButton onClick方法,因爲editext是活動佈局的一部分,將在onClick方法得到空,即。在您的活動方法

// Get the current register EditText objects 
      mEmailRegisterView = (EditText) findViewById(R.id.login_activity_email_register); 
      mPasswordRegisterView = (EditText) findViewById(R.id.login_activity_password_register); 
      mPasswordConfirmRegisterView = (EditText) findViewById(R.id.login_activity_password_confirm_register); 
+0

更改

registerButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { // Get the current register EditText objects mEmailRegisterView = (EditText) findViewById(R.id.login_activity_email_register); mPasswordRegisterView = (EditText) findViewById(R.id.login_activity_password_register); mPasswordConfirmRegisterView = (EditText) findViewById(R.id.login_activity_password_confirm_register); // Store values at the time of the login attempt. mEmailRegister = mEmailRegisterView.getText().toString(); mPasswordRegister = mPasswordRegisterView.getText().toString(); mPasswordConfirmRegister = mPasswordConfirmRegisterView.getText().toString(); Log.d(TAG, "Email : " + mEmailRegister); Log.d(TAG, "Password : " + mPasswordRegister); Log.d(TAG, "Confirm : " + mPasswordConfirmRegister); } }); 

registerButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { // Store values at the time of the login attempt. mEmailRegister = mEmailRegisterView.getText().toString(); mPasswordRegister = mPasswordRegisterView.getText().toString(); mPasswordConfirmRegister = mPasswordConfirmRegisterView.getText().toString(); Log.d(TAG, "Email : " + mEmailRegister); Log.d(TAG, "Password : " + mPasswordRegister); Log.d(TAG, "Confirm : " + mPasswordConfirmRegister); } }); 

,並定義的EditText(以下線)只有一次我其實是有最初。我想可能重新創建EditText對象,當點擊發生將允許我獲得值。沒有任何設置的喜悅。如果這是正確的方法,那麼我會改變我的代碼以反映這一點。我也會添加私人成員。 – RogerSmith