安卓的getText从EditText上返回NULL [英] Android getText from EditText return NULL
问题描述
在我的第一个程序在Android的这个简单的应用程序,我想获得用户名
和密码
从用户。但点击按钮后,将返回 NULL
和多数民众不正确,用户名
和密码
字段有字符串,没有 NULL
。
Textusername
和 Textpassword
在此code是 NULL
并不能得到字符串从 R.id.username
和 R.id.password
我的XML:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
>
<的TextView
机器人:layout_width =0dp
机器人:layout_height =40dp
机器人:layout_weight =1
机器人:文本=@字符串/ EnterUsername
/>
<的EditText
机器人:layout_width =0dp
机器人:layout_height =40dp
机器人:layout_weight =1
机器人:ID =@ + ID /用户名
/>
< / LinearLayout中>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
>
<的TextView
机器人:layout_width =0dp
机器人:layout_height =40dp
机器人:layout_weight =1
机器人:文本=@字符串/ EnterPassword
/>
<的EditText
机器人:layout_width =0dp
机器人:layout_height =40dp
机器人:layout_weight =1
机器人:ID =@ + ID /密码
/>
< / LinearLayout中>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =0dp
机器人:layout_weight =1
/>
<按钮
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID /提交按钮
机器人:文本=@字符串/提交按钮
机器人:重力=中心
机器人:背景=@可绘制/紫/>
< / LinearLayout中>
我的code:
包com.example.AndroidMultiPage;
进口android.app.Activity;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;
公共类MyActivity延伸活动{
/ **
*第一次创建活动时调用。
* /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
最后的EditText的用户名=(EditText上)findViewById(R.id.username);
最后的EditText密码=(EditText上)findViewById(R.id.password);
按钮提交=(按钮)findViewById(R.id.submitButton);
最后弦乐Textusername = username.getText()的toString()。
最后弦乐Textpassword = password.getText()的toString()。
submit.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图查看){
Toast.makeText(
MyActivity.this,Textusername,
Toast.LENGTH_SHORT).show();
//土司SHOW NULL
}
});
}
}
请移动这个
最后弦乐Textusername = username.getText()的toString()。
最后弦乐Textpassword = password.getText()的toString();
在的onClick()
。
in this simple Application of my first program in Android i want to get username
and password
from User. but after click on button that return NULL
and thats incorrect, username
and password
fields have string and are not NULL
.
Textusername
and Textpassword
in this code are NULL
and can not get string from R.id.username
AND R.id.password
My 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>
My Code:
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
}
});
}
}
Please move this
final String Textusername = username.getText().toString();
final String Textpassword = password.getText().toString();
inside onClick()
.
这篇关于安卓的getText从EditText上返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!