我試圖得到一個EditText錯誤來顯示如果EditText中的文本不等於'1'。當我輸入除「1」以外的任何文本時,不會顯示錯誤!這裏是源代碼:EditText錯誤不能正常工作
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = (EditText) findViewById(R.id.edit);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId==EditorInfo.IME_ACTION_NEXT) {
if(editText.getText().toString() != "1") {
editText.setError("Please enter some information!");
}
else {
Toast.makeText(getApplicationContext(), "Not null", Toast.LENGTH_SHORT).show();
}
}
return false;
}
});
}
}
XML代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
我在做什麼錯?任何幫助,將不勝感激!
「==」檢查兩個對象是否引用同一個對象實例。 – mario