来自EditText的Android getText返回NULL

时间:2014-07-23 07:05:18

标签: android android-layout android-activity

在Android中我的第一个程序的这个简单应用程序中,我希望从用户获取usernamepassword。但是点击返回NULL的按钮后,usernamepassword字段会有字符串,而不是NULL。 此代码中的TextusernameTextpasswordNULL,无法从R.id.usernameR.id.password获取字符串

我的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

        <TextView
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="@string/EnterUsername"
                />
        <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:id="@+id/username"
                />
    </LinearLayout>
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

        <TextView
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="@string/EnterPassword"
                />
        <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:id="@+id/password"
                />
    </LinearLayout>
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            />
    <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/submitButton"
            android:text="@string/SubmitButton"
            android:gravity="center"
            android:background="@drawable/purple"/>
</LinearLayout>

我的代码:

package com.example.AndroidMultiPage;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText username = (EditText) findViewById(R.id.username);
        final EditText password = (EditText) findViewById(R.id.password);

        Button   submit   = (Button)   findViewById(R.id.submitButton);

        final String Textusername = username.getText().toString();
        final String Textpassword = password.getText().toString();

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(
                         MyActivity.this, Textusername, 
                         Toast.LENGTH_SHORT).show();

                //TOAST SHOW NULL
            }
        });
    }
}

6 个答案:

答案 0 :(得分:2)

你应该移动

 final String Textusername = username.getText().toString();
 final String Textpassword = password.getText().toString();

在提交onClick(..)的{​​{1}}下和Button内,您应该检查onClick(..)值是否为String。正如@Raghunandan所说......

答案 1 :(得分:1)

移动此

String Textusername = username.getText().toString();
String Textpassword = password.getText().toString();

onClick内。

还要检查

if(!TextUtils.isEmpty(Textusername))
{
           // display Toast 
}

答案 2 :(得分:1)

尝试以下代码:

submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final String Textusername = username.getText().toString();
        final String Textpassword = password.getText().toString();

        Toast.makeText(MyActivity.this, Textusername, Toast.LENGTH_SHORT).show();    
    }
});

答案 3 :(得分:1)

请移动此

final String Textusername = username.getText().toString(); final String Textpassword = password.getText().toString();

onClick()内。

答案 4 :(得分:1)

submit.setOnClickListener(new OnClickListener() {


     @Override
                public void onClick(View view) {

                   String Textusername = username.getText().toString();

                    Toast.makeText(MyActivity.this, Textusername, Toast.LENGTH_SHORT).show();

                    //TOAST SHOW NULL
                }
            });

答案 5 :(得分:0)

Use this:
 submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
String text_user=username.getText().toString();
                Toast.makeText(
                         MyActivity.this, text_user, 
                         Toast.LENGTH_SHORT).show();

                            }
        });