我如何在android中按优先级更新变量

时间:2013-06-19 14:29:19

标签: java android xml sharedpreferences

我尝试在android中开发软键盘,我想在扩展InputmethodeServise的类中使用Xml文件,以使其成为keyboardView的属性,并且我在布局resorces中有几个xml(input1.xml,input2 .xml,input3.xml),并按优先级调用这些xml:

     @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;
                  }
              }

        }

选择xml文件的方法是:

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);

        }
    }

但是当我从prefrnceActivitey中选择任何项目时,xml文件没有改变,这意味着变量(sw)不会更新。我如何通过偏好更新(sw)?请任何人关注我的问题

1 个答案:

答案 0 :(得分:0)

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;

}

并在onStartInputView

中执行此操作
public void onStartInputView(EditorInfo attribute, boolean restarting) {
    super.onStartInputView(attribute, restarting);

    setInputView(onCreateInputView());
}