getText总是返回"" (空)onClick按钮

时间:2015-02-11 22:16:59

标签: java android android-activity

我已经尝试了两个小时来解决这个问题,但我不能。我已经查看了StackOverflow,我尝试通过添加一个viewInflater并在onCreate()中设置edittext来修复它,但没有做任何事情。所以问题是,即使我在每个字段中输入文本String usernameS = txtUserName.getText()。toString();而其他人总是等于""。我找不到解决方案。我在这里错过了什么?感谢

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"/>

2 个答案:

答案 0 :(得分:0)

将所有string.matches(&#34;&#34;)更改为string.equals(&#34;&#34;)。

答案 1 :(得分:0)

删除

setContentView(R.layout.activity_sign_up);

因为你在一个类中调用了两个布局,所以它不起作用并找到那样的按钮;

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