2017-03-07 60 views
0

Github : Stepstone Material StepperStepstone - Material Stepper

有人使用過? 我只想問,我在一個步驟裏面有4個以上的Edittexts,並且每個都有驗證方法。我只想知道在我的活動中應該在哪裏調用這些錯誤捕獲方法?我的驗證方法的

例子是在這裏:

class VerifyAppNameTask extends AsyncTask<String, Void, String> { 
    // use doInBackground() to make network calls, the returned value is 
    // sent to onPostExecute() 
    @Override 
    protected String doInBackground(String... data) { 

     if (data[0].replace(" ","").isEmpty()) 
     { 
      f1 = true; 
      return "1"; 
     } 
     else if (data[0].length() > 25) 
     { 
      f1 = true; 
      return "2"; 
     } 
     else if (checkAppName(data[0])) 
     { 
      f1 = true; 
      return "3"; 
     } 
     else 
     { 
      f1 = false; 
      return "4"; 
     } 


    } 
    @Override 
    protected void onPostExecute(String result) { 
     tilAppName.setErrorEnabled(true); 
     switch(result) 
     { 
      case "1": {tilAppName.setError("You can't leave this empty.");break;} 
      case "2": {tilAppName.setError("Maximum of 25 characters.");break;} 
      case "3": {tilAppName.setError("No spaces allowed");break;} 
      case "4": {tilAppName.setError(null);tilAppName.setErrorEnabled(false);break;} 

     } 

    } 
} 

另一個是這個

public boolean edtAppCategoryET(String data) 
{ 

    tilAppCategory.setErrorEnabled(true); 
    if (data.replace(" ","").equals("")) 
    { 
     tilAppCategory.setError("You can't leave this empty."); 
     return true; 
    } 
    else 
    { 
     tilAppCategory.setError(""); 
     tilAppCategory.setErrorEnabled(false); 
     return false; 
    } 
} 

,這是步進我想你已經代碼

public class AppUploadStep1 extends Fragment implements Step { 

private static final String LAYOUT_RESOURCE_ID_ARG_KEY = "messageResourceId"; 

EditText edtAppName, edtAppVersion, edtAppPlatform, edtAppCategory, edtAppDescription; 
TextInputLayout tilAppName, tilAppVersion, tilAppPlatform, tilAppCategory, tilAppDescription; 
String sAppName, sAppVersion, sAppPlatform, sAppCategory, sAppDescription; 
boolean f1, f2, f3, f4, f5; 

HttpURLConnection connection; 
BufferedReader reader; 
URL url; 
InputStream stream; 
StringBuffer buffer; 
String line; 

ProgressBar loading; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    final View v = inflater.inflate(R.layout.content_app_upload_step1, container, false); 
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 

    //initialize your UI 
    edtAppPlatform = (EditText) v.findViewById(R.id.edtAppPlatform); 
    edtAppVersion = (EditText) v.findViewById(R.id.edtAppVersion); 
    edtAppCategory = (EditText) v.findViewById(R.id.edtAppCategory); 
    edtAppDescription = (EditText) v.findViewById(R.id.edtAppDescription); 
    edtAppName = (EditText) v.findViewById(R.id.edtAppName); 

    tilAppName = (TextInputLayout) v.findViewById(R.id.tilAppName); 
    tilAppVersion = (TextInputLayout) v.findViewById(R.id.tilAppVersion); 
    tilAppPlatform = (TextInputLayout) v.findViewById(R.id.tilAppPlatform); 
    tilAppCategory = (TextInputLayout) v.findViewById(R.id.tilAppCategory); 
    tilAppDescription = (TextInputLayout) v.findViewById(R.id.tilAppDescription); 

    edtAppPlatform.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      showCustomSpinnerDialog(v, R.array.spinner_platform, R.id.edtAppPlatform); 
     } 
    }); 

    edtAppCategory.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      showCustomSpinnerDialog(v, R.array.categories, R.id.edtAppCategory); 
     } 
    }); 

    errorTrap(); 
    return v; 
} 

public static AppUploadStep1 newInstance(@LayoutRes int layoutResId) { 
    Bundle args = new Bundle(); 
    args.putInt(LAYOUT_RESOURCE_ID_ARG_KEY, layoutResId); 
    AppUploadStep1 fragment = new AppUploadStep1(); 
    fragment.setArguments(args); 
    return fragment; 
} 



@Override 
public VerificationError verifyStep() { 


// getData(); 
    //if (edtAppVersionET(sAppVersion)) { 
     // return true 
    // ?new VerificationError("Password cannot be empty") 
    // :null; 
return null; 
} 

@Override 
public void onSelected() { 

} 

@Override 
public void onError(@NonNull VerificationError error) { 

} 

回答

0

得到了答案,但這裏是我所做的:

在verifystep方法中檢查您的edittext的條件並在末尾返回VerificationError(「」)(它位於kotlin)

if(editText?.text?.toString().isNullOrEmpty()){ 
    editText?.setError("this field is required") 
    editText?.addTextChangedListener(object : TextWatcher { 

     override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} 

     override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { 
       editText?.setError("this field is required") 
       } 

     override fun afterTextChanged(s: Editable) { 
       editText?.setError(null) 
      } 
     }) 
     return VerificationError("") 
}