在Android Studio中制作必填的EditText字段

时间:2018-12-31 00:45:10

标签: java android android-studio

我开始制作一个应用。我目前有一个EditText。我该怎么做?什么都没有输入到EditText中!消息Please Enter a username应该在屏幕上闪烁,但仍转到下一个场景/活动。如果长度为0,如何停止提交。我将return false放入公共空白,但收到以下消息cannot return a value from a method with void result type

    public void sendMessage(View view){
    Intent intent = new Intent(this,DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();

    //Trim whitespace
    message = message.trim();

    //Checks if the message has anything.
    if (message.length() == 0)
    {
        editText.setError("Please Enter a username!");
        //return false;
    }
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);
}

5 个答案:

答案 0 :(得分:2)

尝试

string text=editText.getText().toString().trim();
if (TextUtils.isEmpty(text)){
            editText.setError("Please Enter a username!");
        }else {
            //do something
        }

答案 1 :(得分:1)

代替... some code for j = 0 to nprocs-1 displs[j] = (int)j*global_size/nprocs; counts[j] = (int)(global_size(j+1)*/nprocs)-(int)(global_size*j/nprocs); ... test (checks), this ensures the best size distribution ... all arrays are pointers with allocated sizes as per above MPI_Scatterv(global_array,counts,displs,type, local_array,counts[rank],type,root,comm); ... begins loop (every rank does this whole loop) ... some calculations, inner loops divided between ranks MPI_Allgatherv(local_array,counts[rank],type, global_array,counts,displs,type,comm); ... some calculations, inner loops divided between ranks ,只写没有返回类型的return false;(因为该方法的返回类型为return;),它应该让您离开该方法。

答案 2 :(得分:1)

按如下所示修改功能。

Joins

答案 3 :(得分:1)

使用ifelse子句将实现您想要的。

//Checks if the message has anything.
if (message.length() == 0)
{
    editText.setError("Please Enter a username!");
    //return false;
} else {
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);
}

答案 4 :(得分:0)

尝试一下

public void sendMessage(View view){
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
if (!emptyEdittext(editText, "Please Enter a username!")) 
{
  intent.putExtra(EXTRA_MESSAGE,message);
  startActivity(intent);
}}

2。调用以下方法进行验证

public boolean emptyEdittext(EditText editText, String msg)
{
    /*check edittext length*/
    if(editText.getText().toString().length()==0)
    {
        Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}