2014-02-18 39 views
2

我已經閱讀了關於這個問題的很多答案,但我的問題是問我在哪裏放置代碼。我期望驗證edittextPreference中的數字大於100。這是我的代碼用於填充preferecnes:驗證editTextPreference Android

public class SettingsFrag extends PreferenceFragment{ 

    //Override onCreate so that the code will run when the activity is started. 
    @Override 
    public void onCreate(Bundle savedInstanceState){   
      //Call to the super class. 
      super.onCreate(savedInstanceState); 

      //add the preferences from the XML file. 
      addPreferencesFromResource(R.xml.preferences); 
    } 

}

它是在這裏,我添加驗證或我會創建另一個類?

的preferences.xml:

<EditTextPreference    
     android:key="geofence_range"    
     android:title="Geofence Size"    
     android:defaultValue="500"  
     android:inputType="number" 
     android:summary="Geofence Size Around User Location"    
     android:dialogTitle="Enter Size (meters):" /> 

回答

8

EditTextPreference添加setOnPreferenceChangeListeneraddPreferencesFromResource後驗證數據輸入用戶:

EditTextPreference edit_Pref = (EditTextPreference) 
        getPreferenceScreen().findPreference("geofence_range"); 
    edit_Pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 

    @Override 
     public boolean onPreferenceChange(Preference preference, Object newValue) { 
      // put validation here.. 
      if(<validation pass>){ 
       return true; 
      }else{ 
       return false; 
      } 
     } 
    }); 
4

咦。另一個應該在Android中變得很容易的事情是,但事實並非如此。其他答案只是默默地阻止將結果寫回到首選項,這似乎有點低劣。 (顯示烤麪包的次數較少,但仍然是粗製濫造)。

您需要自定義首選項才能執行此操作。自定義onValidate以滿足您的需求。

package com.two_play.extensions; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.preference.EditTextPreference; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.EditText; 

public class ValidatingEditTextPreference extends EditTextPreference { 
    public ValidatingEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    public ValidatingEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public ValidatingEditTextPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ValidatingEditTextPreference(Context context) { 
     super(context); 
    } 

    @Override 
    protected void showDialog(Bundle state) { 
     super.showDialog(state); 
     AlertDialog dlg = (AlertDialog)getDialog(); 
     View positiveButton = dlg.getButton(DialogInterface.BUTTON_POSITIVE); 
     getEditText().setError(null); 
     positiveButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       onPositiveButtonClicked(v); 
      } 
     }); 
    } 

    private void onPositiveButtonClicked(View v) { 
     String errorMessage = onValidate(getEditText().getText().toString()); 
     if (errorMessage == null) 
     { 
      getEditText().setError(null); 
      onClick(getDialog(),DialogInterface.BUTTON_POSITIVE); 
      getDialog().dismiss(); 
     } else { 
      getEditText().setError(errorMessage); 
      return; // return WITHOUT dismissing the dialog. 
     } 
    } 

    /*** 
    * Called to validate contents of the edit text. 
    * 
    * Return null to indicate success, or return a validation error message to display on the edit text. 
    * 
    * @param text The text to validate. 
    * @return An error message, or null if the value passes validation. 
    */ 
    public String onValidate(String text) 
    { 
     try { 
      Double.parseDouble(text); 
      return null; 
     } catch (Exception e) 
     { 
      return getContext().getString(R.string.error_invalid_number); 
     } 
    } 
}