0

我的 xml

<TextView 
        android:id="@+id/newPlaceLatLable"
        android:text="Place Latitude"
        android:layout_width="150dp"
        android:layout_height="40dp"
       android:layout_below="@id/newPlaceLable"/>
    <EditText
        android:id="@+id/newPlaceLat"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/newPlaceLatLable" 
         android:layout_below="@id/newPlaceLable"
         android:inputType="numberDecimal"
        />
    <TextView 
        android:id="@+id/newPlaceLonLable"
        android:text="Place Longitude"
        android:layout_width="150dp"
        android:layout_height="40dp"
       android:layout_below="@id/newPlaceLatLable"/>
    <EditText
        android:id="@+id/newPlaceLon"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/newPlaceLonLable" 
        android:layout_below="@id/newPlaceLatLable"
        android:inputType="numberDecimal"
        />

并在代码中

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();

其中 et1、et2 和 et3 是

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);

但是每次我运行应用程序时,它都会吐司“Latitute不能为空”。虽然 et2 里面有文字,但是这里没有显示。请看一下。什么问题

4

4 回答 4

2

您可以按照@blackbelt 的建议或像这样使用..

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

希望这可以帮助...

于 2013-05-19T09:15:03.693 回答
1

你想检查它EditText是否填充了相同的值。

matches("")

不是你需要的。

检查lenght字符串是否 > 0,或使用equals("")

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();
于 2013-05-19T09:09:08.403 回答
1

用这种方式

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();
于 2013-05-19T09:12:39.487 回答
1

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

https://stackoverflow.com/a/9700152/2240535

于 2013-05-19T09:28:20.110 回答