【问题标题】:How to change background color or theme of keys dynamically in Custom Keyboard Android如何在自定义键盘 Android 中动态更改键的背景颜色或主题
【发布时间】:2013-08-13 08:38:21
【问题描述】:

我正在开发自定义键盘应用程序,我需要在我的应用程序中设置或更改键盘的背景主题或颜色 .their setting.xml 视图,用户可以在其中为键行选择不同的背景主题和不同的颜色。 在第一次启动应用程序时它工作正常,但下次自定义键盘显示主题时不会更改。

我正在使用此代码:

public class SoftKeyboard extends InputMethodService 
    implements KeyboardView.OnKeyboardActionListener {
static final boolean DEBUG = false;

/**
 * This boolean indicates the optional example code for performing
 * processing of hard keys in addition to regular text generation
 * from on-screen interaction.  It would be used for input methods that
 * perform language translations (such as converting text entered on 
 * a QWERTY keyboard to Chinese), but may not be used for input methods
 * that are primarily intended to be used for on-screen text entry.
 */
static final boolean PROCESS_HARD_KEYS = true;

private static final int SELECT_PICTURE = 101;

private KeyboardView mInputView;
private CandidateView mCandidateView;
private CompletionInfo[] mCompletions;
private Context context = SoftKeyboard.this;
private StringBuilder mComposing = new StringBuilder();
private boolean mPredictionOn;
private boolean mCompletionOn;
private int mLastDisplayWidth;
private boolean mCapsLock;
private long mLastShiftTime;
private long mMetaState;

private LatinKeyboard mSymbolsKeyboard;
private LatinKeyboard mSymbolsShiftedKeyboard;
private LatinKeyboard mQwertyKeyboard;
private LatinKeyboard mSmilyKeyboard;
private LatinKeyboard mSmilyKeyboard1;

private LatinKeyboard mCurKeyboard;

private String mWordSeparators;

/**
 * Main initialization of the input method component.  Be sure to call
 * to super class.
 */

@Override 
   public void onCreate() {
    super.onCreate();

    mWordSeparators = getResources().getString(R.string.word_separators);      
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String name = preferences.getString("Name","");


    Log.e("SoftKeyboard - ",""+name+"OnCreate Method Called--");



    if(!name.equalsIgnoreCase(""))
    {
      name = name+"  Sethi";  /* Edit the value here*/
    }
}

这是我设置或选择颜色或主题的设置类:

public class Setting extends Activity implements OnClickListener {

    LinearLayout roar, edge, burst, impact, blue_theme, orange_theme,
            green_theme, black_brigthness, white_brightness;

    Bundle bundle;

    public static boolean isblackBrightness = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.setting);

        // ----------UI intilisation---------------------
        uiInitilisation();

    }

    public void uiInitilisation() {

        roar = (LinearLayout) findViewById(R.id.ror_LL);
        edge = (LinearLayout) findViewById(R.id.edge_LL);
        burst = (LinearLayout) findViewById(R.id.burst_LL);
        impact = (LinearLayout) findViewById(R.id.impact_LL);

        // -------------Themes------------------------------
        blue_theme = (LinearLayout) findViewById(R.id.blue_theme_LL);
        orange_theme = (LinearLayout) findViewById(R.id.orange_theme_LL);
        green_theme = (LinearLayout) findViewById(R.id.green_theme_LL);

        // ------------Brightness----------------------------
        black_brigthness = (LinearLayout) findViewById(R.id.black_brigthness_LL);
        white_brightness = (LinearLayout) findViewById(R.id.white_brigthness_LL);

        // --------------On Click Events-------------------

        roar.setOnClickListener(this);
        edge.setOnClickListener(this);
        burst.setOnClickListener(this);
        impact.setOnClickListener(this);

        // -----------Theme-------------------------------------
        blue_theme.setOnClickListener(this);
        orange_theme.setOnClickListener(this);
        green_theme.setOnClickListener(this);

        // ------------------Brightness--------------------------
        black_brigthness.setOnClickListener(this);
        white_brightness.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.ror_LL:

            startActivity(new Intent(Setting.this, MainActivity.class));

            break;

        case R.id.edge_LL:

            startActivity(new Intent(Setting.this, MainActivity.class));




            break;
        case R.id.burst_LL:

            startActivity(new Intent(Setting.this, MainActivity.class));

            break;

        case R.id.impact_LL:

            startActivity(new Intent(Setting.this, MainActivity.class));

            break;  

        case R.id.blue_theme_LL:

            Intent i = new Intent(Setting.this,
                    MainActivity.class);
                i.putExtra("color", "blue");

            startActivity(i);


            break;

        case R.id.orange_theme_LL:
            Intent i2 = new Intent(Setting.this,
                    MainActivity.class);
                i2.putExtra("color", "orange");

            startActivity(i2);

            break;

        case R.id.green_theme_LL:
            Intent i3 = new Intent(Setting.this,
                    MainActivity.class);
                i3.putExtra("color", "green");

            startActivity(i3);

            break;
        case R.id.black_brigthness_LL:

            Intent black_britness = new Intent(Setting.this,
                    MainActivity.class);
            black_britness.putExtra("bright", "black");
            startActivity(black_britness);

              SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
              SharedPreferences.Editor editor = preferences.edit();
              editor.putString("Name","Harneet");
              editor.commit();


            isblackBrightness = true ; 
            Log.e("Black--","Black=="+isblackBrightness);

            break;
        case R.id.white_brigthness_LL:
            Intent white_britness = new Intent(Setting.this,
                    MainActivity.class);
            white_britness.putExtra("bright", "white");
            startActivity(white_britness);
            isblackBrightness = false;
            Log.e("white--","White=="+isblackBrightness);

            SharedPreferences preferences1 = PreferenceManager.getDefaultSharedPreferences(this);
              SharedPreferences.Editor editor1 = preferences1.edit();
              editor1.putString("Name","Arun");
              editor1.commit();


            break;

         }
    }
}

我不知道是否必须设置小部件。

【问题讨论】:

    标签: android android-softkeyboard


    【解决方案1】:

    当显示键盘时,框架调用onStartInputView。您可以对该函数进行编程以查看共享首选项的值并在键盘视图上适当地设置颜色/主题。

    【讨论】:

    • 我如何根据选择的颜色设置不同的键颜色我的意思是我是哪个类。在软键盘类中?
    • 您使用的是默认键盘视图还是自定义键盘视图?如果您使用自己的,它应该很容易,但如何做将取决于您如何实现它。如果您使用的是默认键盘-我认为实际上没有任何严肃的键盘使用它。它似乎没有太多的 UI 自定义能力。 xml 允许您指定关键图像,但它似乎没有运行时函数来执行此操作。
    • 我正在使用默认键盘视图,但我想在用户第一次选择黑色时更改键的 beckgroud 图像,如果用户选择白色或背景图像,则颜色将变为黑色,如果白色控件返回 onStartInputView 方法,但在这里更改颜色整个布局我想更改特定图像
    • 我正在使用 mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.input, null);这用于膨胀键盘布局
    【解决方案2】:

    获取更改自定义键盘布局的解决方案。

    当键盘第一次加载时调用 onCreateInputView()。之后,每次打开键盘时都会调用 onStartInputView(EditorInfo 属性,布尔重启)。

    所以,现在keyboard(Theme) 的布局必须在onCreateInputView() 中定义像这样

    public KeyboardView mInputView;
    public View onCreateInputView() {
    
        SharedPreferences pre = getSharedPreferences("test", 1);
        int theme = pre.getInt("theme", 1);
    
        if(theme == 1)
        {
            this.mInputView = (KeyboardView) this.getLayoutInflater().inflate(R.layout.input, null);
        }else
        {
            this.mInputView = (KeyboardView) this.getLayoutInflater().inflate(R.layout.input_2, 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());
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-03
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 2013-03-25
      • 2012-04-01
      • 2019-03-22
      • 2018-01-11
      • 1970-01-01
      相关资源
      最近更新 更多