gpt4 book ai didi

java - EditText 错误不起作用

转载 作者:行者123 更新时间:2023-12-01 17:16:41 25 4
gpt4 key购买 nike

如果 EditText 中的文本不等于“1”,我会尝试显示 EditText 错误。当我输入“1”以外的任何文本时,不会出现错误!这是源代码:

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" />

我究竟做错了什么?任何帮助将不胜感激!

最佳答案

这就是错误:

editText.getText().toString() != "1"

你需要做:

!(editText.getText().toString().equals("1"));

关于java - EditText 错误不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21581005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com