2015-09-04 60 views
2

我是新來的android和製作一個應用程序,其中有縱向和橫向模式的佈局設計。該應用在屏幕方向上運行良好。 在這個程序中我已經設置了編輯文本的驗證並顯示錯誤槽set Error()。這工作正常,但是當我嘗試旋轉編輯文本中的空白字段的方向設置錯誤()方法設置錯誤 。我試圖通過谷歌搜索以不同的方式解決這個問題,但沒有成功。請幫幫我。下面感謝Android屏幕方向編輯文本顯示設置錯誤?

是我的代碼

EditText name, email, phone_no, subject, message; 
    Button send; 
    JSONObject json; 
    final Context context = this; 
    String valid_name = " ", valid_phone_no = " ", valid_email = " ", 
      valid_sub = " ", valid_msg = " "; 
    private ProgressDialog pDialog; 
    Boolean isInternetPresent = false; 
    Boolean isrotaion = false; 
    static boolean et_focus; 
    String emailPattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
    JsonParser jsonParser = new JsonParser(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.contact_us); 
     name = (EditText) findViewById(R.id.edtname); 
     email = (EditText) findViewById(R.id.edtemail); 
     phone_no = (EditText) findViewById(R.id.edtphone); 
     subject = (EditText) findViewById(R.id.edtsubject); 
     message = (EditText) findViewById(R.id.edtmessage); 
     send = (Button) findViewById(R.id.send); 

     ActionBar actionBar = getActionBar(); 

     actionBar.setDisplayShowHomeEnabled(false); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setHomeButtonEnabled(true); 
     actionBar.setDisplayShowTitleEnabled(true); 
     actionBar.setBackgroundDrawable(new ColorDrawable(Color 
       .parseColor("#0f567c"))); 
     setTitle("Contact us"); 


     send.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       if (valid_name != null && valid_email != null 
         && valid_phone_no != null && valid_sub != null 
         && valid_msg != null) { 
        isInternetPresent = isConnected(); 
        if (!isInternetPresent) { 

         buildAlertMessageNonet(); 
        } else 

        { 
         new CreateContact().execute(); 

        } 
       } else { 
        Toast.makeText(getApplicationContext(), 
          "Please Fill up all Fields Correctly.", 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 

     }); 


     int orientation = this.getResources().getConfiguration().orientation; 
     if (orientation == Configuration.ORIENTATION_PORTRAIT) { 
      // code for portrait mode 
      isrotaion = true; 
     } else { 
      // code for landscape mode 
      isrotaion = true; 
     } 

     name.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 

      } 

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

       validtion_name(name); 

      } 
     }); 
     email.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 
       Log.i("TAG", "betextchange"); 
      } 

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

       validation_Email(email); 

      } 
     }); 
     phone_no.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 

      } 

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

       validation_Phone_no(10, 13, phone_no); 

      } 
     }); 
     subject.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 

      } 

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

       validation_Subject(subject); 

      } 
     }); 

     message.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 

      } 

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

       validation_Message(message); 

      } 
     }); 
    } 

    public void validtion_name(EditText edt) throws NumberFormatException { 
     Log.i("TAG", "name"); 

     Log.i("TAG", String.valueOf(edt.length())); 
     if (edt.getText().toString().trim().length() <= 0) { 
      edt.setError("Accept Alphabets Only."); 
      valid_name = null; 
     } else if (edt.getText().toString().contains(" ")) { 
      edt.setError(null); 
     } else if (!edt.getText().toString().matches("[a-zA-Z]+")) { 
      edt.setError("Accept Alphabets Only."); 
      valid_name = null; 
     } else if (edt.getText().toString().charAt(0) == ' ') { 
      edt.setError("First Letter Not be Space "); 
      valid_name = null; 

     } else { 
      valid_name = edt.getText().toString(); 
     } 

    } 

    public void validation_Phone_no(int MinLen, int MaxLen, EditText edt) 
      throws NumberFormatException { 

     if (edt.getText().toString().length() <= 0) { 
      edt.setError("Numbers Only"); 
      valid_phone_no = null; 
     } else if (edt.getText().toString().length() < MinLen) { 
      edt.setError("Minimum length " + MinLen); 
      valid_phone_no = null; 

     } else if (edt.getText().toString().length() > MaxLen) { 
      edt.setError("Maximum length " + MaxLen); 
      valid_phone_no = null; 

     } else if (edt.getText().toString().charAt(0) == ' ') { 
      edt.setError("First Number Not be Space"); 
      valid_phone_no = null; 
     } else if (!edt.getText().toString().matches("^[+]?[0-9]{10,13}$")) { 
      edt.setError("Invalid Number"); 
      valid_phone_no = null; 

     } 

     else { 
      valid_phone_no = edt.getText().toString(); 

     } 

    } 

    public void validation_Email(EditText edt) { 

     if (edt.getText().toString() == null) { 
      edt.setError("Invalid Email Address"); 
      valid_email = null; 
     } else if (isEmailValid(edt.getText().toString()) == false) { 
      edt.setError("Invalid Email Address"); 
      valid_email = null; 
     } else if (edt.getText().toString().charAt(0) == ' ') { 
      edt.setError("First Letter Not be Space"); 
      valid_email = null; 
     } else if (!edt.getText().toString().matches(emailPattern)) { 
      edt.setError("Invalid Email Address"); 
      valid_email = null; 
     } else { 
      valid_email = edt.getText().toString(); 
     } 
    } 

    boolean isEmailValid(CharSequence email) { 
     return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches(); 
    } 

    public void validation_Subject(EditText edt) { 
     if (edt.getText().toString().isEmpty()) { 
      edt.setError("Subject not be Empty "); 
      valid_sub = null; 
     } else if (edt.getText().toString().charAt(0) == ' ') { 
      edt.setError("First Letter Not be Space"); 
      valid_sub = null; 
     } else { 
      valid_sub = edt.getText().toString(); 
     } 
    } 

    public void validation_Message(EditText edt) { 
     Log.i("TAG", "maessage"); 
     if (edt.getText().toString().isEmpty()) { 
      edt.setError("Message not be Empty "); 
      valid_msg = null; 

     } else if (edt.getText().toString().charAt(0) == ' ') { 
      edt.setError("First Letter Not be Space"); 
      valid_msg = null; 
     } else { 
      edt.setError(null); 
      valid_msg = edt.getText().toString(); 
     } 
    } 

此外,在清單

<application 
     android:allowBackup="true" 
     android:icon="@drawable/icon" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.my.Home" 
      android:configChanges="orientation" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.my.Careers" 
      android:configChanges="orientation" 
      android:windowSoftInputMode="stateHidden" > 
     </activity> 
     <activity 
      android:name="com.my.Services" 
      android:configChanges="orientation" > 
     </activity> 
     <activity 
      android:name="com.my.Contact_us" 
      android:configChanges="orientation|keyboardHidden" 
      android:windowSoftInputMode="stateHidden" > 
     </activity> 
     <activity 
      android:name="com.my.View_service" 
      android:configChanges="orientation" > 
     </activity> 
     <activity 
      android:name="com.my.Direction" 
      android:configChanges="orientation" > 
     </activity> 

回答

0

每當你改變所有的意見再生方向定義orination。 OnCreateView方法再次被調用,並且TextChangedListener再次被添加到editText視圖中,該視圖調用它的方法並最終在空白editText上調用setError。 要檢查屏幕方向是否已更改,並且沒有在EditText中輸入任何內容,我們可以使用在OnCreate()方法中傳遞的Bundle,如果活動是新創建的,則此包爲空,但當屏幕方向發生更改時空值。 查看下面的代碼,我已經實現了它的名稱EditText只,但我想你將不得不做同樣的所有EditText Views,因爲它取決於哪個視圖光標是方向改變。 聲明一個束作爲一個類的成員如,

Bundle mSavedInstanceState; 

並保存通過束在它的OnCreate()方法,

 if(savedInstanceState != null){ 
     this.mSavedInstanceState = savedInstanceState; 
    } 

現在同時檢查錯誤使用該束中,如果它的取向變化(即捆綁將不會爲空)不顯示錯誤... 用下面的代碼替換您的代碼, PSSome條件錯了,我已經修復它們, 希望它有助於..! 讓我知道它是否適用於您,並將其標記爲答案,以便對其他人有用。

public void validtion_name(EditText edt) throws NumberFormatException { 
    Log.i("TAG", "name"); 

    Log.i("TAG", String.valueOf(edt.length())); 
    if (edt.getText().toString().trim().length() <= 0 && mSavedInstanceState ==null) { 
     edt.setError("Accept Alphabets Only."); 
     valid_name = null; 
    } else if (edt.getText().toString().contains(" ")) { 
     edt.setError(null); 
    } else if (edt.getText().toString().trim().length() > 0 && !edt.getText().toString().matches("[a-zA-Z]+")) { 
     edt.setError("Accept Alphabets Only."); 
     valid_name = null; 
    } else if (edt.getText().toString().length() > 0) { 
     if(edt.getText().toString().charAt(0) == ' '){ 
      edt.setError("First Letter Not be Space "); 
      valid_name = null; 
     } 
    } else { 
     valid_name = edt.getText().toString(); 
    } 
    } 
+0

謝謝@rj但問題沒有解決。 – Techiee

+0

檢查我編輯的答案 –