gpt4 book ai didi

Android EditText setError() 无法按预期工作

转载 作者:行者123 更新时间:2023-11-29 14:52:44 26 4
gpt4 key购买 nike

我在 EditText 上遇到 setError() 问题。打开 Activity 时,它会检查某些字段是否为空,如果为真,则在其上设置错误消息。但是,感叹号图标仅在我在字段中写入一些文本然后将其删除时才会显示。如果我失去对该字段的关注,该图标将再次消失。 NaamTelefonnumer 这两个字段都有此验证。

我使用 Android 2.2.2 SDK 并且应用程序在 Nexus 7 上运行并进行了最新更新。

我有 Util 类:

public class Util {

private static String TAG = "Util Class";

public static boolean editTextIsEmpty(EditText edittext) {
if (edittext.getText().toString().trim().length() < 1)
return true;
else
return false;
}

public void editTextListener(final EditText editText) {
editText.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (editTextIsEmpty(editText) && editText.isEnabled())
editText.setError("Nodig");
else
editText.setError(null);
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (editTextIsEmpty(editText) && editText.isEnabled())
editText.setError("Nodig");
else
editText.setError(null);
}
});
}
}

然后我的 Activity 中有方法 validateInput():

public class DeliveryActivity extends BaseActivity {

private ImageButton btnSetDate;

private Button btnToSummary;
private Button btnSearchAddress;

private EditText txtPostcode;
private EditText txtHouseNumber;
private EditText txtHouseNumberSuffix;
private EditText txtStreet;
private EditText txtCity;
private EditText txtDeliveryDate;
private EditText txtName;
private EditText txtPhone;
private EditText txtEmail;
private EditText txtRemark;

private TextView lblExtraDeliveryInfo;

private Spinner spinnerDelivery;
private Spinner spinnerDeliveryPeriod;
private Spinner spinnerContact;
private Spinner spinnerDeliveryAddress;
private Spinner spinnerExtraDeliveryInfo;

private RelativeLayout rlDeliveryAddressDetails;

private DevRestHelper additionalDeliveryInfo;
private DevRestHelper searchClientAddress;

private Util util = new Util();

private int year;
private int month;
private int day;

public static final int DIALOG_DATEPICKER = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delivery);

initControls();

validateInput();
}

private void initControls() {
btnSetDate = (ImageButton) findViewById(R.id.activity_delivery_btnCalendar);

btnToSummary = (Button) findViewById(R.id.activity_delivery_btnSummary);
btnSearchAddress = (Button) findViewById(R.id.activity_delivery_btnSearchAddress);

spinnerDelivery = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryMethod);
spinnerDeliveryPeriod = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryPeriod);
spinnerContact = (Spinner) findViewById(R.id.activity_delivery_spinnerContactperson);
spinnerDeliveryAddress = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryAddress);
spinnerExtraDeliveryInfo = (Spinner) findViewById(R.id.activity_delivery_spinnerExtraDeliveryInformation);

txtPostcode = (EditText) findViewById(R.id.activity_delivery_txtPostcode);
txtHouseNumber = (EditText) findViewById(R.id.activity_delivery_txtHousenumber);
txtHouseNumberSuffix = (EditText) findViewById(R.id.activity_delivery_txtHousenumberSuffix);
txtStreet = (EditText) findViewById(R.id.activity_delivery_txtStreet);
txtCity = (EditText) findViewById(R.id.activity_delivery_txtCity);
txtDeliveryDate = (EditText) findViewById(R.id.activity_delivery_txtDeliveryDate);
txtName = (EditText) findViewById(R.id.activity_delivery_txtName);
txtPhone = (EditText) findViewById(R.id.activity_delivery_txtPhone);
txtEmail = (EditText) findViewById(R.id.activity_delivery_txtEmail);
txtRemark = (EditText) findViewById(R.id.activity_delivery_txtRemark);

lblExtraDeliveryInfo = (TextView) findViewById(R.id.activity_delivery_lblExtraDetailInformation);

rlDeliveryAddressDetails = (RelativeLayout) findViewById(R.id.activity_delivery_rlDeliveryAddressDetails);
}

private void validateInput() {
util.editTextListener(txtPostcode);
util.editTextListener(txtHouseNumber);
util.editTextListener(txtDeliveryDate);
}
}

我只想说代码在 BlueStacks 模拟器上工作。

enter image description here

最佳答案

Jelly Bean_MR1(4.2 和 4.2.1)上的 setError 存在一个已知错误。然而,我假设您正在测试的 Nexus 7 正在运行这些版本的 Android 之一。看这里:http://code.google.com/p/android/issues/detail?id=40417

当您将焦点放在该 EditText 字段上时将显示错误,但当您失去焦点时,错误图标将不可见以通知用户该问题。

关于Android EditText setError() 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13414473/

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