2015-02-11 44 views
0

我一直在嘗試兩個小時來解決這個問題,但我不能。我查看了StackOverflow,並試圖通過添加viewInflater並在onCreate()中設置edittext來修復它,但沒有任何內容。所以問題是,即使我在每個字段中輸入文本字符串usernameS = txtUserName.getText()。toString();其他人總是等於「」。我找不到解決方案。我在這裏錯過了什麼?由於getText總是返回「」(空)onClick按鈕

package com.awesometeam.liftm8; 

import android.content.Intent; 
import android.graphics.LightingColorFilter; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.awesometeam.liftm8.data.User; 


public class SignUpActivity extends ActionBarActivity { 

    private UserDataSource m_DataSource; 
    private View signupView; 
    private EditText txtUserName,txtPassword,txtPhone,txtEmail,txtPasswordConfirmation; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_sign_up); 
     Button btnSignUp = (Button)this.findViewById(R.id.btnSignUp); 
     btnSignUp.getBackground().setColorFilter(new LightingColorFilter(0xFF0000FF, 0xFF0000FF)); 

     signupView = getLayoutInflater().inflate(R.layout.activity_sign_up, null,false); 

     txtUserName = (EditText) signupView.findViewById(R.id.txtNomUsager); 
     txtPassword = (EditText) signupView.findViewById(R.id.txtMotPasse); 
     txtPhone = (EditText) signupView.findViewById(R.id.txtTelephone); 
     txtEmail = (EditText) signupView.findViewById(R.id.txtEmail); 
     txtPasswordConfirmation = (EditText) signupView.findViewById(R.id.txtMotPasseConfirmation); 

     this.m_DataSource = new UserDataSource(this); 
     this.m_DataSource.open(); 
    } 

    @Override 
    protected void onStart() { 
     this.m_DataSource.open(); 
     super.onStart(); 
    } 


    @Override 
    protected void onStop() { 
     this.m_DataSource.close(); 
     super.onStop(); 
    } 

    public void onClickInscription(View source) { 

     String usernameS = txtUserName.getText().toString(); 
     String phoneS = txtPhone.getText().toString(); 
     String emailS = txtEmail.getText().toString(); 
     String passwordS = txtPassword.getText().toString(); 
     String passwordConfirmationS = txtPasswordConfirmation.getText().toString(); 

     if (usernameS.matches("") || phoneS.matches("")|| 
       emailS.matches("")|| passwordS.matches("") 
       || passwordConfirmationS.matches("")) { 
      Toast.makeText(this, "All fields must be filled", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     else if (!(passwordS.matches(passwordConfirmationS))) { 
      Toast.makeText(this, "Both passwords aren't identical", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     else{ 
      m_DataSource.insert(new User(usernameS,passwordS,emailS,phoneS)); 
      finish(); 
     } 
    } 
} 

我的XML

<Button 
    android:layout_marginTop="20dp" 
    android:layout_centerInParent="true" 
    android:layout_width="fill_parent" 
    android:layout_height="60dp" 
    android:text="@string/title_activity_sign_up" 
    android:id="@+id/btnSignUp" 
    android:onClick="onClickInscription" 
    android:layout_below="@+id/txtTelephone"/> 
+0

哼...我有點累了,但我沒有看到你設置的點擊監聽器。 – jvrodrigues 2015-02-11 22:20:33

+0

編輯我的帖子,在XML中調用onClick。它進入我調試它的函數,它只是總是空的字符串。它總是進入第一個if。 – 2015-02-11 22:24:55

+0

在XML字段'onClick' – Josef 2015-02-11 22:26:21

回答

0

改變所有string.matches( 「」),以string.equals( 「」)。

+0

或TextUtils.isEmpty(phoneS)等。 – Gaskoin 2015-02-11 22:52:30

+0

這不會改變任何東西,問題出現在條件 – 2015-02-11 22:53:39

+0

之前它不是這個或edittexts是空的。 – jvrodrigues 2015-02-11 23:05:11

0

刪除

setContentView(R.layout.activity_sign_up); 

因爲你叫一個類中的兩個佈局,這是行不通的,找到這樣的按鈕;

Button btnSignUp = (Button)signupView.findViewById(R.id.btnSignUp);