我的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中有文字,但它没有在这里显示。请好好看看。有什么问题
答案 0 :(得分: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();
希望这会有所帮助...
答案 1 :(得分:1)
您要检查EditText
是否填充了相同的值。
matches("")
不是你需要的。
检查字符串的lenght
是否为&gt; 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();
答案 2 :(得分: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();
答案 3 :(得分:1)
match()&amp;之间的区别equal()是matches()检查String与正则表达式模式的匹配,而equal()将此Comparator与指定的Object进行比较并指示它们是否相等,请参阅此答案