Android EditText始终显示空值

Android EditText始终显示空值,android,xml,android-edittext,Android,Xml,Android Edittext,我的xml 其中et1、et2和et3是 if (et1.getText().toString().matches("")) Toast.makeText(getApplicationContext(), "Name can't be null", Toast.LENGTH_SHORT).show(); else if (et2.getText().toString().matches("")) Toast.makeText(get

我的xml

其中et1、et2和et3是

if (et1.getText().toString().matches(""))
            Toast.makeText(getApplicationContext(), "Name can't be null", Toast.LENGTH_SHORT).show();
        else if (et2.getText().toString().matches(""))
            Toast.makeText(getApplicationContext(), "Latitute can't be null", Toast.LENGTH_SHORT).show();
        else  if (et3.getText().toString().matches(""))
            Toast.makeText(getApplicationContext(), "Longitute can't be null", Toast.LENGTH_SHORT).show();

但每次我运行应用程序时,它都会给出toast“Latitute不能为null”。虽然et2中有文本,但这里没有显示。请看一下。有什么问题

您想检查它吗
编辑文本
中填充了相同的值

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change);
        et1 = (EditText) findViewById(R.id.newPlaceText);        
         et2 = (EditText) findViewById(R.id.newPlaceLat);
         et3 = (EditText) findViewById(R.id.newPlaceLon);
这不是你需要的

检查字符串的
长度是否大于0,或者使用
等于(“”


如果(et1.getText().toString().length()您想检查它,
EditText
将填充相同的值

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change);
        et1 = (EditText) findViewById(R.id.newPlaceText);        
         et2 = (EditText) findViewById(R.id.newPlaceLat);
         et3 = (EditText) findViewById(R.id.newPlaceLon);
这不是你需要的

检查字符串的
长度是否大于0,或者使用
等于(“”

if(et1.getText().toString().length()使用这种方式

if (et1.getText().toString().length() <= 0)
            Toast.makeText(getApplicationContext(), "Name can't be null", Toast.LENGTH_SHORT).show();
else if (et2.getText().toString().length() <= 0)
            Toast.makeText(getApplicationContext(), "Latitute can't be null", Toast.LENGTH_SHORT).show();
else  if (et3.getText().toString().length() <= 0)
            Toast.makeText(getApplicationContext(), "Longitute can't be null", Toast.LENGTH_SHORT).show();
if(et1.getText().toString().trim().length()使用此方法

if (et1.getText().toString().length() <= 0)
            Toast.makeText(getApplicationContext(), "Name can't be null", Toast.LENGTH_SHORT).show();
else if (et2.getText().toString().length() <= 0)
            Toast.makeText(getApplicationContext(), "Latitute can't be null", Toast.LENGTH_SHORT).show();
else  if (et3.getText().toString().length() <= 0)
            Toast.makeText(getApplicationContext(), "Longitute can't be null", Toast.LENGTH_SHORT).show();

if(et1.getText().toString().trim().length()您可以按照@blackbelt的建议或类似的方式使用

if (et1.getText().toString().trim().length() <= 0)
            Toast.makeText(getApplicationContext(), "Name can't be null", Toast.LENGTH_SHORT).show();
else if (et2.getText().toString().trim().length() <= 0)
            Toast.makeText(getApplicationContext(), "Latitute can't be null", Toast.LENGTH_SHORT).show();
else  if (et3.getText().toString().trim().length() <= 0)
            Toast.makeText(getApplicationContext(), "Longitute can't be null", Toast.LENGTH_SHORT).show();

希望这有帮助……

你可以按照@blackbelt的建议或这样做

if (et1.getText().toString().trim().length() <= 0)
            Toast.makeText(getApplicationContext(), "Name can't be null", Toast.LENGTH_SHORT).show();
else if (et2.getText().toString().trim().length() <= 0)
            Toast.makeText(getApplicationContext(), "Latitute can't be null", Toast.LENGTH_SHORT).show();
else  if (et3.getText().toString().trim().length() <= 0)
            Toast.makeText(getApplicationContext(), "Longitute can't be null", Toast.LENGTH_SHORT).show();

希望这有帮助……

matches()&equal()之间的区别在于,matches()检查字符串与正则表达式模式的匹配,而equal()将此比较器与指定对象进行比较,并指示它们是否相等,请参见此答案


matches()&equal()之间的区别在于,matches()检查字符串与正则表达式模式的匹配,而equal()将此比较器与指定对象进行比较,并指示它们是否相等,请参见此答案


这是完整的xml代码。这里只显示两个edittext。这是完整的xml代码。这里只显示两个edittext。仍然不工作。Actullay et2.getText()始终为空,事件中有文本。它不能为空,同时也有文本。我的意思是。当我在应用程序中输入文本时。这很奇怪。et1显示它有文本,但et2没有。你的最后一句对我来说没有意义,希望你没有使用
et2=new EditText(上下文)
在代码中的任何地方,这都会更改引用..只需猜测一下..仍然不起作用。Actullay et2.getText()始终为空,事件中有文本。它不能为空,同时也有文本。我的意思是。当我在应用程序中输入文本时。这很奇怪。et1显示它有文本,但et2没有。你的最后一句对我来说没有意义,希望你没有使用
et2=new EditText(上下文)
在代码中的任何地方,这都会更改引用..只需一次猜测。。。。。