簡體   English   中英

如何在自定義鍵盤 Android 中動態更改鍵的背景顏色或主題

[英]How to change background color or theme of keys dynamically in Custom Keyboard Android

我正在開發自定義鍵盤應用程序我需要在我的應用程序中設置或更改鍵盤的背景主題或顏色 .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;

         }
    }
}

無論我是否必須設置小部件,我都不知道如何做到這一點。

當鍵盤顯示時,框架調用onStartInputView 您可以對該函數進行編程以查看共享首選項的值並在鍵盤視圖上適當地設置顏色/主題。

獲取更改自定義鍵盤布局的解決方案。

當鍵盤第一次加載時 onCreateInputView() 被調用。 之后鍵盤打開時每次調用 onStartInputView(EditorInfo attribute, boolean restarting) 。

所以,現在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());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM