我在android中有5个EditTexts。我想知道我是否可以检查所有5个EditTexts是否为空。有什么办法可以做到吗?


当前回答

if(TextUtils.isEmpty(textA.getText())){
            showToast(it's Null");
        }

你可以使用TextUtils。isEmpty就像我的例子! 祝你好运

其他回答

如果editText为空,试试另一种简单的方法:

String star = editText.getText().toString();

if (star.equalsIgnoreCase("")) {
  Toast.makeText(getApplicationContext(), "Please Set start no", Toast.LENGTH_LONG).show();
}

以下几点对我来说很有用:

if(searchText.getText().toString().equals("")) 
    Log.d("MY_LOG", "Empty");

首先,我从EditText检索文本,然后将其转换为字符串,最后使用.equals方法将其与“”进行比较。

如果EditText为空,为什么不禁用按钮呢?恕我直言,这看起来更专业:

        final EditText txtFrecuencia = (EditText) findViewById(R.id.txtFrecuencia);  
        final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleStartStop);
        txtFrecuencia.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            toggle.setEnabled(txtFrecuencia.length() > 0);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

        }
       });

使用TextUtils。isEmpty(“文本”);对于单行代码

EditText txtUserID = (EditText) findViewById(R.id.txtUserID);

String UserID = txtUserID.getText().toString();

if (UserID.equals("")) 

{
    Log.d("value","null");
}

您将在LogCat....中看到该消息