简体   繁体   中英

Trying to create a Android Soft Keyboard from an activity

I wrote a activity for a keyboard. Now I am moving a class that extends Activity to another that exteds InputMethodService ... And I am missing findViewById to create the keyboard layout. Because I have not enough knoledge to create a SoftKeyboard I include the Java code. My next step will be to modify the AndroidManifest.xml to be a service, I guess.

package com.keyboard.mine;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends InputMethodService /* Activity */ {
    ...
    ...
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main, menu);
//        return true;
//    }


    private void nuevas_teclas (String [][] teclas_array) {
        Button boton = (Button) ***findViewById***(R.id.button00);
        boton.setText(teclas_array [pagina][0]);
        boton = (Button) ***findViewById***(R.id.button01);
        boton.setText(teclas_array [pagina][1]);
        boton = (Button) ***findViewById***(R.id.button02);
        boton.setText(teclas_array [pagina][2]);
        boton = (Button) ***findViewById***(R.id.button10);
        boton.setText(teclas_array [pagina][4]);
        boton = (Button) ***findViewById***(R.id.button11);
        boton.setText(teclas_array [pagina][5]);
        boton = (Button) ***findViewById***(R.id.button12);
        boton.setText(teclas_array [pagina][6]);
        boton = (Button) ***findViewById***(R.id.button20);
        boton.setText(teclas_array [pagina][8]);
        boton = (Button) ***findViewById***(R.id.button21);
        boton.setText(teclas_array [pagina][9]);
        boton = (Button) ***findViewById***(R.id.button22);
        boton.setText(teclas_array [pagina][10]);   
        boton = (Button) ***findViewById***(R.id.button31);
        if (! teclas_array [pagina][13].equals ("")) {
            boton.setText(teclas_array [pagina][13]);
        }
        boton = (Button) ***findViewById***(R.id.buttonaux0);
        if (! teclas_array [pagina][3].equals ("")) {
            boton.setText(teclas_array [pagina][3]);
        }
        boton = (Button) ***findViewById***(R.id.buttonaux1);
        if (! teclas_array [pagina][7].equals ("")) {
            boton.setText(teclas_array [pagina][7]);
        }
        boton = (Button) ***findViewById***(R.id.buttonaux2);
        if (! teclas_array [pagina][11].equals ("")) {
            boton.setText(teclas_array [pagina][11]);
        }
        boton = (Button) ***findViewById***(R.id.buttonaux3);
        if (! teclas_array [pagina][15].equals ("")) {
            boton.setText(teclas_array [pagina][15]);
        }
        boton = (Button) ***findViewById***(R.id.buttonaux4);
        if (! teclas_array [pagina][14].equals ("")) {
            boton.setText(teclas_array [pagina][14]);
        }
        boton = (Button) ***findViewById***(R.id.buttonaux5);
        if (! teclas_array [pagina][12].equals ("")) {
            boton.setText(teclas_array [pagina][12]);
        }
}
...
... 
...
}

You can check following source codes available :

Scandinavian-keyboard
Hackers Keyboard
Android Emoji Keyboard

And then try to make on your own.
Hope this helps you and you can get some basic idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM