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 can't be null」というトーストが表示されます。et2 にはテキストがありますが、ここには表示されませんでした。どうぞご覧ください。何が問題なの

4

4 に答える 4

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

match() と equal() の違いは、matches() が文字列と正規表現パターンの一致をチェックするのに対し、equal() はこのコンパレータを指定されたオブジェクトと比較し、それらが等しいかどうかを示すことです。この回答を参照してください。

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

于 2013-05-19T09:28:20.110 に答える