android -> input method development (getting started)


  

Reprinted from: http://blog.csdn.net/le_go/article/details/9264831#comments

 

 

Reference documentation: http://developer.android.com/guide/topics/text/creating-input-method.html

 

Steps to implement the input method:

The core of the input method program is a service class, which must inherit from InputMethodService.

Let's take a look at the steps to implement a basic input method program.

(1) Create a class that inherits from android.inputmethodservice.InputMethodService, called the service class of the input method.

(2) Configure this service class in the AndroidManifest.xml file.

(3) Write a layout file for displaying the soft keyboard.

(4) Override the onCreateInputView method of the InputMethodService class.

(5) The onCreateInputView method needs to return the View object corresponding to the layout file created in step 3. Before returning, it is generally necessary to set the event of the corresponding control, such as the soft keyboard button click event.

(6) Write code that responds to key events in the soft keyboard in the input method service class or other classes, such as button click events, physical keyboard events, and so on.

 

Let's implement a simple input method program.

Step 1: Create a new Android project named simple_inputmethod. The directory structure is as follows:

 

 

 

 

Step 2: Create an AndroidInputMethodService class, which inherits from InputMethodService, and configure it in the Android.Manifest.xml file:

AndroidInputMethodService class:

 

package net.csdn.leigo.inputmethod;  
  
import net.csdn.leigo.inputmethod.R;  
import android.inputmethodservice.InputMethodService;  
import android.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.inputmethod.EditorInfo;  
import android.view.inputmethod.InputConnection;  
import android.widget.Button;  
  
public class AndroidInputMethodService extends InputMethodService implements  
        OnClickListener {  
}

 

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="net.csdn.leigo.inputmethod"  
    android:versionCode="1"  
    android:versionName="1.0" >  
  
    <uses-sdk  
        android:minSdkVersion="10"  
        android:targetSdkVersion="10" />  
  
    <application  
        android:allowBackup="true"  
        android:icon="@drawable/ic_launcher"  
        android:label="@string/app_name"  
        android:theme="@style/AppTheme" >  
  
        <!-- Optional: an activity for controlling the IME settings -->  
        <activity android:name="net.csdn.leigo.inputmethod..InputMethodSetting" >  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
            </intent-filter>  
        </activity>  
        <!-- Declares the input method service -->  
        <service  
            android:name="net.csdn.leigo.inputmethod.AndroidInputMethodService"  
            android:permission="android.permission.BIND_INPUT_METHOD" >  
            <intent-filter>  
                <action android:name="android.view.InputMethod" />  
            </intent-filter>  
  
            <meta-data  
                android:name="android.view.im"  
                android:resource="@xml/method" />  
        </service>  
    </application>  
  
</manifest>  

 It must be set when configuring the input method service

 

 

 

android.permission.BIND_INPUT_METHOD"  

 permissions, and add a <intent-filter> tag

"android.view.InputMethod"  

 

A <meta-data> tag is also added to the <service> tag to configure the input method, that is, the input method we wrote can be seen in the "Language and Keyboard" setting interface, in which the android:resource attribute specifies

An input method resource ID. This resource file (method.xml) is in the res\xml directory, and the code is as follows:

 

<?xml version="1.0" encoding="UTF-8"?>  
<input-method xmlns:android="http://schemas.android.com/apk/res/android"  
    android:settingsActivity="net.csdn.leigo.inputmethod.InputMethodSetting"/>  

 

The android:settingActivity attribute of the <input-method> tag can define the input method setting window.

 

InputMethodSetting.java:

package net.csdn.leigo.inputmethod;  
  
import android.app.Activity;  
import android.os.Bundle;  
  
public class InputMethodSetting extends Activity {  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate (savedInstanceState);  
        setContentView(R.layout.setting);  
    }  
  
}

 

setting.xml:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="horizontal" >  
  
    <TextView  
        android:id="@+id/textview"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="input method setting window" />  
  
</LinearLayout>

 

 

 

Step 3: Write a layout file. This layout file is actually the layout of the soft keyboard. There are 5 horizontally arranged buttons in this layout, the first 4 are used to input 4 strings (that is, the value of the android:text attribute of the <Button> tag), and the last button is used to hide the soft keyboard:

keyboard.xml:

 

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="horizontal" >  
  
    <Button  
        android:id="@+id/btn1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="a" />  
  
    <Button  
        android:id="@+id/btn2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="6dp"  
        android:text="b" />  
  
    <Button  
        android:id="@+id/btn3"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="6dp"  
        android:text="c" />  
  
    <Button  
        android:id="@+id/btn4"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="6dp"  
        android:text="d" />  
  
    <Button  
        android:id="@+id/btn_hide"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="6dp"  
        android:text="Hide" />  
  
</LinearLayout>  

 

Step 4: Modify the code of the AndroidInputMethodService class, first overwrite the onCreateInputView method, then load the keyboard and xml layout files in the onCreateInputView method, and set the click event of the button, and finally return the View object of the soft keyboard.

 

 

 

@Override  
    public View onCreateInputView() {  
        // Load the keyboard.xml file  
        View view = getLayoutInflater().inflate(R.layout.keyboard, null);  
        // Set the click event of the 5 buttons in the layout  
        view.findViewById(R.id.btn1).setOnClickListener(this);  
        view.findViewById(R.id.btn2).setOnClickListener(this);  
        view.findViewById(R.id.btn3).setOnClickListener(this);  
        view.findViewById(R.id.btn4).setOnClickListener(this);  
        view.findViewById(R.id.btn_hide).setOnClickListener(this);  
        Log.d(TAG, "onCreateInputView()");  
        // return the View object  
        return view;  
    }  

 

Note: The input method interface (soft keyboard) does not allow us to create an Activity by ourselves. This Activity is provided by the system, and we only need the View object displayed on the Activity, which is the return value of the onCreateInputView method.

 

 

The last one needs to handle the click action of the button, the AndroidInputMethodService class needs to implement the OnClickListener interface.

@Override  
public void onClick(View v) {  
	if (v.getId() == R.id.btn_hide) {  
		// hide the soft keyboard  
		hideWindow();  
	} else {  
		Button button = (Button) v;  
		// Get the InputConnection object  
		InputConnection inputConnection = getCurrentInputConnection();  
		if (button.getId() == R.id.btn1) {  
			// set type-ahead text  
			// The second parameter value of the setComposingText method is 1, indicating that the text is pre-entered at the current position  
			inputConnection.setComposingText(button.getText(), 1);  
		} else if (button.getId() == R.id.del) {  
			// delete characters, the first parameter deletes the first few characters of the cursor, and the second parameter deletes the characters after the cursor
			// deleteSurroundingText(int beforeLength, int afterLength)
			inputConnection.deleteSurroundingText(1, 0);
		}else {  
			// Output text to the currently focused EditText control  
			// The second parameter value of the commitText method is 1, which means to insert text at the current position  
			inputConnection.commitText(button.getText(), 1);  
		}  
	}
	
	// setInputView(view); Switch the displayed keyboard layout view
}  

 

 

Type-ahead text is available through the InoutConnection.setComposingText method. Type-ahead text generally displays an underline below the typed text until it is replaced with the typed text, otherwise the underline will remain.

 

 

Run the project to view the renderings:



 

 

 
 


After testing, if the inputType property of EditText is the default, click can input, but if it is other such as phone or email, click has no effect. This can refer to the documentation or will be introduced in the next article.

-------------------------------

 

Thanks again to the original author for your selfless sharing
 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326565399&siteId=291194637