2013-08-14 13 views
7

Sto lavorando su Custom Keyboard ApplicazioneCome impostare diversi sfondo di tasti per la tastiera personalizzata Android

This is background color of key when user select blue

If user select green this should be background color

Questo è il codice per il colore di sfondo di input.xml in SoftKeyboard: -

 @Override 
    public View onCreateInputView() { 


     Log.e("onStartInputView ","On StartInput View Called--"); 

     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
     String Backgroundcolour = preferences.getString("BackgroundColour",""); 

    Log.e("Brithnesss- -","----"+Backgroundcolour); 

    if(Backgroundcolour.equalsIgnoreCase("black")) 
    { 

    this.mInputView = (KeyboardView) getLayoutInflater().inflate(
      R.layout.input, null); 


    }else 
    { 
     this.mInputView = (KeyboardView) getLayoutInflater().inflate(
      R.layout.input1, null); 
     //this.mInputView.setB 
    } 

    this.mInputView.setOnKeyboardActionListener(this); 
    this.mInputView.setKeyboard(this.mQwertyKeyboard); 
    return this.mInputView; 
} 

@Override public void onStartInputView(EditorInfo attribute, boolean restarting) { 
    super.onStartInputView(attribute, restarting); 
    // Apply the selected keyboard to the input view. 

    setInputView(onCreateInputView()); 

} 

Non riesco a ottenere come impostare l'immagine di sfondo per una chiave specifica.

+0

Consulta questo http://stackoverflow.com/questions/15789997/how-to-ch ange-background-color-of-key-per-android-soft-keyboard –

+0

Non riesco a ottenere questo modulo in modo corretto, per favore, dimmi come impostare le diverse chiavi dei colori? – user

+0

È necessario impostare lo sfondo per lo sfondo chiave specifico anziché la stessa immagine di sfondo chiave per l'intera tastiera. ho bisogno urgente – user

risposta

7

Per fare un esempio, c'è un small downloadable project che crea una tastiera numerica personalizzato. Alla classe CustomKeyboardView lì o alla tua classe di tastiera personalizzata, aggiungi un metodo come il seguente. Sostituisce il metodo onDraw() e disegna lo sfondo della chiave definita con il codice 7 (in questo caso lo "0") rosso e tutti gli altri tasti blu.

@Override 
public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    List<Key> keys = getKeyboard().getKeys(); 
    for (Key key : keys) {    
     if (key.codes[0] == 7) { 
      Log.e("KEY", "Drawing key with code " + key.codes[0]); 
      Drawable dr = (Drawable) context.getResources().getDrawable(R.drawable.red_tint); 
      dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); 
      dr.draw(canvas); 

     } else { 
      Drawable dr = (Drawable) context.getResources().getDrawable(R.drawable.blue_tint); 
      dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); 
      dr.draw(canvas); 
     }    
    } 
} 

tinted keys

In questo caso, non ho usato immagini 9-patch, ma solo alcune semplici 50% immagini quadrati trasparenti e raggiunto un effetto per cui i pulsanti esistenti sono semplicemente tinti con i colori che ricercato. Per ottenere un risultato più personalizzato, potrei rendere le mie immagini drawable a 9 patch e fare quanto segue. Nota che i due tasti con le icone non vengono visualizzati correttamente perché le icone non sono definite come immagini a 9 patch e non ho fatto alcuno sforzo particolare per consentire loro di scalare bene per questo esempio. Inoltre non ho affrontato l'uso di immagini/effetti diversi per i vari stati per le chiavi; altri hanno mostrato come farlo.

@Override 
public void onDraw(Canvas canvas) { 
    // super.onDraw(canvas); 

    List<Key> keys = getKeyboard().getKeys(); 
    for (Key key : keys) { 
     if (key.codes[0] == 7) { 
      NinePatchDrawable npd 
       = (NinePatchDrawable) context.getResources().getDrawable(R.drawable.red_key); 
      npd.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); 
      npd.draw(canvas); 

     } else { 
      NinePatchDrawable npd 
       = (NinePatchDrawable) context.getResources().getDrawable(R.drawable.blue_key); 
      npd.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); 
      npd.draw(canvas); 
     } 

     Paint paint = new Paint(); 
     paint.setTextAlign(Paint.Align.CENTER); 
     paint.setTextSize(48); 
     paint.setColor(Color.GRAY); 

     if (key.label != null) { 
      canvas.drawText(key.label.toString(), key.x + (key.width/2), 
          key.y + (key.height/2), paint); 
     } else { 
      key.icon.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); 
      key.icon.draw(canvas); 
     } 
    } 
}  

replaced keys

+0

come cambiare lo sfondo popup a livello di programmazione quando si preme a lungo il tasto della vista tastiera personalizzata? –

+0

Il collegamento fornito nella risposta non risponde alla domanda. –

+0

@MaihanNijat, non era previsto da solo; è solo una comodità che offre una tastiera personalizzata in stile abbastanza standard. Il codice esplicitamente aggiunto nella risposta dimostra una modifica semplicistica a quel progetto (o al codice simile fornito nei documenti ufficiali e in altri post di S/O) per rispondere alla domanda dell'OP. – scottt

1

Per semplificare, è necessario creare la classe MyKeyboardView e eseguire alcune modifiche simili.

public class MyKeyboardView extends android.inputmethodservice.KeyboardView { 

    Context context; 
    public MyKeyboardView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
     this.context = context ; 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     Paint paint = new Paint(); 
     Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/Hippie.otf"); 
     paint.setTypeface(font); 
     paint.setTextSize(40); 

     List<Key> keys = getKeyboard().getKeys(); 
     for(Key key: keys) { // int i = 0 ; switch(i) and implement your logic 

     if(key.pressed){ 
      NinePatchDrawable npd = (NinePatchDrawable)context.getResources().getDrawable(R.drawable.glow); 
      npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height); 
      npd.draw(canvas); 
      if(key.label != null) 
       canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint); 
     }else if(key.modifier){ // boolean that defines key is function key 
      NinePatchDrawable npd = (NinePatchDrawable)context.getResources().getDrawable(R.drawable.btn_keyboard_special); 
      npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height); 
      npd.draw(canvas); 
      if(key.label != null) 
       canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint); 
     } 


     break; 
    } 
} 
2

Ho creato un'applicazione della tastiera in cui io uso la proprietà KeyBackground in KeyboardView, in questo modo:

<KeyboardView android:keyBackground="@drawable/buttonbgselector" .../> 

Per fare questo in modo dinamico Io uso il seguente codice:

@Override 
public View onCreateInputView() { 
    mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.input, null); 
    mInputView.setBackgroundResource(R.drawable.buttonbgselector); 
    mInputView.setOnKeyboardActionListener(this); 
    mInputView.setKeyboard(mQwertyKeyboard); 
    return mInputView; 
} 
Problemi correlati