简体   繁体   中英

How I can update variable by preference in android

I try to develop soft keyboard in android, I am want to use Xml file in the class that is extends InputmethodeServise in order to make it as attributes to keyboardView , and I have several xml in layout resorces (input1.xml,input2.xml,input3.xml), and call those xml by preference:

     @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                String key) {
            if(key.equals("textColor"))
            {
             String selectedColor = sharedPreferences.getString(key, "green");
             if (selectedColor.equals("red"))
             {

                 sw=1;

     }

             if (selectedColor.equals("blue"))
             {

                 sw=2;
                  }
                if (selectedColor.equals(" yellow"))
             {

                 sw=3;
                  }
              }

        }

And the method that select xml file is:

int sw;

 @Override public void onCreate() {
         prefs = PreferenceManager.getDefaultSharedPreferences(this);
         prefs.registerOnSharedPreferenceChangeListener(this);

        super.onCreate();

        switch(sw)
        { case 1:
 mInputView = (LatinKeyboardView) getLayoutInflater().inflate(R.layout.input1, null);
             break;
        case 2:
    mInputView = (LatinKeyboardView) getLayoutInflater().inflate(R.layout.input1, null);
      break;
    case 3: mInputView = (LatinKeyboardView) getLayoutInflater().inflate(R.layout.input2, null);
break
default:
 mInputView = (LatinKeyboardView) getLayoutInflater().inflate(R.layout.input, null);

        }
    }

But when I select any Item from prefrnceActivitey, the xml file not changed , this means the variable (sw) not update. How I can update (sw) by prefernce?? please any one follow my issue

public KeyboardView mInputView; public View onCreateInputView() {

SharedPreferences pre = getSharedPreferences("test", sw);
int theme = pre.getInt("theme", sw);

if(theme == 1)
{
    this.mInputView = (KeyboardView) this.getLayoutInflater().inflate(R.layout.input1, null);
}else
{
    this.mInputView = (KeyboardView) this.getLayoutInflater().inflate(R.layout.input2, null);

}
this.mInputView.setOnKeyboardActionListener(this);
this.mInputView.setKeyboard(this.mQwertyKeyboard);
return this.mInputView;

}

and do this in onStartInputView

public void onStartInputView(EditorInfo attribute, boolean restarting) {
    super.onStartInputView(attribute, restarting);

    setInputView(onCreateInputView());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM