簡體   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