【问题标题】:How I can update variable by preference in android我如何在android中按偏好更新变量
【发布时间】:2013-06-19 14:29:19
【问题描述】:

我尝试在 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)?请任何人关注我的问题

【问题讨论】:

    标签: java android xml sharedpreferences


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 2011-12-09
      • 1970-01-01
      相关资源
      最近更新 更多