繁体   English   中英

如何在Android中按喜好更新变量

[英]How I can update variable by preference in android

我尝试在android中开发软键盘,我想在扩展InputmethodeServise的类中使用Xml文件,以使其成为keyboardView的属性,并且我在布局资源中有几个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)? 请任何人关注我的问题

公共KeyboardView mInputView; 公共视图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());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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