Android修改输入法Enter的文本

EditText设置Enter的文字有以下属性:


一:布局设法(android:imeOptions="actionSearch" android:singleLine="true"):

android:singleLine="true"这个必须要加,不然不会生效。
  1. actionUnspecified  未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.
  2. actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 
  3. actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 
  4. actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 
  5. actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND
  6. actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT
  7. actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE

二:代码设法:

et.setSingleLine();这个必须要加,不然不会生效。

  1. et1.setImeOptions(EditorInfo.IME_ACTION_UNSPECIFIED);
  2. et1.setImeOptions(EditorInfo.IME_ACTION_NONE);
  3. et1.setImeOptions(EditorInfo.IME_ACTION_GO);
  4. et1.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
  5. et1.setImeOptions(EditorInfo.IME_ACTION_SEND);
  6. et1.setImeOptions(EditorInfo.IME_ACTION_NEXT);
  7. et1.setImeOptions(EditorInfo.IME_ACTION_DONE);


Android修改输入法Enter的文本_第1张图片Android修改输入法Enter的文本_第2张图片Android修改输入法Enter的文本_第3张图片

上面效果出来了,那么我们能不能来自定义这个按钮的事件呢?答案是肯定的:
关键代码:
 <EditText
        android:id="@+id/et7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="搜索"
        android:imeOptions="actionSearch"
        android:singleLine="true" />



et7.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				boolean isClick = event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER;
				if (actionId == EditorInfo.IME_ACTION_SEARCH || isClick) {
					return true;
				}
				return false;
			}
		});

下面是完整代码:
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/et1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="未指定"
        android:imeOptions="actionUnspecified"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="没有动作"
        android:imeOptions="actionNone"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="去往"
        android:imeOptions="actionGo"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="搜索"
        android:imeOptions="actionSearch"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="发送"
        android:imeOptions="actionSend"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="下一个"
        android:imeOptions="actionNext"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="完成"
        android:imeOptions="actionDone"
        android:singleLine="true" />

</LinearLayout>


代码文件:

package com.xiaoxi.entertext;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

@SuppressLint({ "UseSparseArrays", "CutPasteId" })
public class MainActivity extends Activity implements OnEditorActionListener {

	// EditText通过设置android:imeOptions来改变默认的”文本或者样式。这里举几个常用的常量值:
	// actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.
	// actionNone没有动作,对应常量EditorInfo.IME_ACTION_NONE
	// actionGo 去往,对应常量EditorInfo.IME_ACTION_GO
	// actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH
	// actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND
	// actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT
	// actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE

	private Map<Integer, Boolean> enterTypeMap;
	private Map<Integer, String> enterTextMap;
	private EditText et1;
	private EditText et2;
	private EditText et3;
	private EditText et4;
	private EditText et5;
	private EditText et6;
	private EditText et7;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		initView();
		initData();
		setListener();
	}

	private void initView() {
		et1 = (EditText) findViewById(R.id.et1);
		et2 = (EditText) findViewById(R.id.et2);
		et3 = (EditText) findViewById(R.id.et3);
		et4 = (EditText) findViewById(R.id.et4);
		et5 = (EditText) findViewById(R.id.et5);
		et6 = (EditText) findViewById(R.id.et6);
		et7 = (EditText) findViewById(R.id.et7);
	}

	private void initData() {
		enterTypeMap = new HashMap<Integer, Boolean>();
		enterTypeMap.put(EditorInfo.IME_ACTION_UNSPECIFIED, true);// 未指定
		enterTypeMap.put(EditorInfo.IME_ACTION_NONE, true);// 没有动作
		enterTypeMap.put(EditorInfo.IME_ACTION_GO, true);// 去往
		enterTypeMap.put(EditorInfo.IME_ACTION_SEARCH, true);// 搜索
		enterTypeMap.put(EditorInfo.IME_ACTION_SEND, true);// 发送
		enterTypeMap.put(EditorInfo.IME_ACTION_NEXT, true);// 下一个
		enterTypeMap.put(EditorInfo.IME_ACTION_DONE, true);// 完成

		enterTextMap = new HashMap<Integer, String>();
		enterTextMap.put(EditorInfo.IME_ACTION_UNSPECIFIED, "未指定");
		enterTextMap.put(EditorInfo.IME_ACTION_NONE, "没有动作");
		enterTextMap.put(EditorInfo.IME_ACTION_GO, "去往");
		enterTextMap.put(EditorInfo.IME_ACTION_SEARCH, "搜索");
		enterTextMap.put(EditorInfo.IME_ACTION_SEND, "发送");
		enterTextMap.put(EditorInfo.IME_ACTION_NEXT, "下一个");
		enterTextMap.put(EditorInfo.IME_ACTION_DONE, "完成");
	}

	private void setListener() {
		et1.setOnEditorActionListener(this);
		et2.setOnEditorActionListener(this);
		et3.setOnEditorActionListener(this);
		et4.setOnEditorActionListener(this);
		et5.setOnEditorActionListener(this);
		et6.setOnEditorActionListener(this);
		et7.setOnEditorActionListener(this);
	}

	/**
	 * 监听输入法Enter的单击事件
	 */
	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		String searchStr = v.getText().toString().trim();
		Log.i("TEST", "输入的内容---" + searchStr);
		if (enterTypeMap.get(actionId) || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
			Log.i("TEST", "文本框的类型---" + enterTextMap.get(actionId));
			return true;
		}
		return false;
	}

}

偷懒?看不懂?不想写?我懂你——>>源码

你可能感兴趣的:(Android修改输入法Enter的文本)