1

这里有一些问题。我创建了一个 EditText 数组,它工作正常。现在,当其中一个 EditText 为空时出现错误。这是我的代码:

int[] textIDs = new int[] {R.id.etFirstName, R.id.etLastName, R.id.etEmail, R.id.etAddress, R.id.etCity, R.id.etRegion, R.id.etMobile, R.id.etLandLine, R.id.etYear, R.id.etMonth, R.id.etDay };
        for(int j=0; j<textIDs.length; j++) {
            EditText editText = (EditText) findViewById(textIDs[j]);
            if(editText.getText().toString().trim().equals(""))
            {
                 // editText is empty
                Toast.makeText(MainActivity.this, "Request cannot performed..\n Please ensure all fields are filled", Toast.LENGTH_SHORT).show();
                break;
            } 
            else 
            {
                 // editText is not empty
                Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT).show();
            }

使用此代码的主要问题是循环继续执行此代码 Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT).show(); 在每个循环中继续显示的功能。循环完成后,这个 Toast 有什么显示方式吗?

4

3 回答 3

1

把它放在for循环之外..

boolean isNotEmpty = false;

    for(int j=0; j<textIDs.length; j++) {
                EditText editText = (EditText) findViewById(textIDs[j]);
                if(editText.getText().toString().trim().equals(""))
                {
                     // editText is empty
                    Toast.makeText(MainActivity.this, "Request cannot performed..\n Please ensure all fields are filled", Toast.LENGTH_SHORT).show();

                    isNotEmpty = false;   // Marking as Empty
                    break;
                } 
                else 
                {
                     // editText is not empty

                    isNotEmpty = true     // Marking as Non-Empty

                }
             }
    if (isNotEmpty){
    Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT).show();
    }
于 2012-08-14T02:18:19.203 回答
0
    boolean doShowToast = false;
    int[] textIDs = new int[] {R.id.etFirstName, R.id.etLastName, R.id.etEmail, R.id.etAddress, R.id.etCity, R.id.etRegion, R.id.etMobile, R.id.etLandLine, R.id.etYear, R.id.etMonth, R.id.etDay };
    for(int j=0; j<textIDs.length; j++) {
        EditText editText = (EditText) findViewById(textIDs[j]);
        if(editText.getText().toString().trim().equals(""))
        {// editText is empty
            Toast.makeText(MainActivity.this, "Request cannot performed..\n Please ensure all fields are filled", Toast.LENGTH_SHORT).show();
            break;
        } 
        else 
        {
             // editText is not empty
            doShowToast = true;
        }
    }
    if(doShowToast){
         Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT).show();
    }

通过使用布尔值,您只会在循环退出后显示 else 语句的 toast 一次,并且只有在调用 else 语句时,所以只有当“editText 不为空”时

于 2012-08-14T02:18:47.303 回答
0
int[] textIDs = new int[] {R.id.etFirstName, R.id.etLastName, R.id.etEmail, R.id.etAddress, R.id.etCity, R.id.etRegion, R.id.etMobile, R.id.etLandLine, R.id.etYear, R.id.etMonth, R.id.etDay };
Toast t; 

    for(int j=0; j<textIDs.length; j++) {
        EditText editText = (EditText) findViewById(textIDs[j]);
        if(editText.getText().toString().trim().equals(""))
        {// editText is empty
            t = Toast.makeText(MainActivity.this, "Request cannot performed..\n Please ensure all fields are filled", Toast.LENGTH_SHORT);
            break;
        } 
        else 
        {
             // editText is not empty
            static Toast toast = Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT);
            t = toast;
        }
    }
    t.show();
于 2012-08-14T02:25:55.887 回答