Android Studio 软键盘的监听事件setOnEditorActionListener

最近在用AS做登录界面使用EditText的时候,发现了一个监听事件setOnEditorActionListener;因为通过布局文件中的imeOptions可以控制软件盘右下角的按钮显示为不同按钮。所以和EditorInfo搭配起来可以实现各种软键盘的功能。

各种属性对应:

  • imeOptions=”actionUnspecified” –> EditorInfo.IME_ACTION_UNSPECIFIED
  • imeOptions=”actionNone” –> EditorInfo.IME_ACTION_NONE
  • imeOptions=”actionGo” –> EditorInfo.IME_ACTION_GO
  • imeOptions=”actionSearch” –> EditorInfo.IME_ACTION_SEARCH
  • imeOptions=”actionSend” –> EditorInfo.IME_ACTION_SEND
  • imeOptions=”actionNext” –> EditorInfo.IME_ACTION_NEXT
  • imeOptions=”actionDone” –> EditorInfo.IME_ACTION_DONE

    用法

    • 布局中定义一个EditText控件
    • 定义一个可编辑的editText控件

        android:id="@+id/name_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:background="@null"
        android:hint="请输入账户"
        android:textSize="14sp"
        android:ems="10"
        android:inputType="number"
        android:imeOptions="actionDone" /> 
    • 添加setOnEditorActionListener方法

    • //定义监听事件
      nameEdit.setOnEditorActionListener(this);
    • /*
      * EditText是TextView子类
      * 方法:onEditorAction()
      * 第一个参数:TextView textView 表示当前触发事件的EditText的对象,类似于textView=findViewById(R.id.name_edit)
      * 第二个参数:int actionId 表示 按下“完成按钮”,这里和xml文件中EditText属性imeOptions对应
      *             但要注意actionId是指软盘上的,而键盘上的actionId与软盘上的不一样;即在软盘上“完成按钮”的actionId为0,而键盘上的“完成按钮(回车键)”的actionId为6
      * 第三个参数:KeyEvent keyEvent 表示 按下“完成按钮”,这里和xml文件中EditText属性imeOptions对应,但keyEvent里面的内容更丰富,内容如下:
      *             keyEvent.toString()=KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ENTER, scanCode=28, metaState=0, flags=0x8, repeatCount=0, eventTime=5664212, downTime=5664212, deviceId=1, source=0x301 }
      * 返回值:返回true,保留软键盘;false,隐藏软键盘
      */
      @Override
      public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
          Toast.makeText(login_activitys.this, textView.getText().toString(), Toast.LENGTH_LONG).show();
          Log.i(LOG_TEST,actionId+"|"+EditorInfo.IME_ACTION_DONE);
          if (actionId == EditorInfo.IME_ACTION_DONE) {   // 按下完成按钮,这里和xml文件中的EditText中属性imeOptions对应;
              Toast.makeText(login_activitys.this, "测试成功了!", Toast.LENGTH_LONG).show();
              return false;   //返回true,保留软键盘;false,隐藏软键盘
          }
          return false;
      }


下面我写了一个实例——登录界面,里面包含了该监听事件

xml源代码:

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@color/lightGray">


            android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:src="@mipmap/head"/>


            android:layout_width="match_parent"
        android:layout_height="33dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/imageView"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:background="#ffffff"
        android:orientation="horizontal"
        android:id="@+id/linearLayout1">

                    android:id="@+id/name_lab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="20dp"
            android:textSize="18dp"
            android:text="账号:"
            android:textAlignment="center"/>

                    android:id="@+id/name_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@null"
            android:hint="请输入账户"
            android:textSize="14sp"
            android:ems="10"
            android:inputType="number"
            android:imeOptions="actionDone" /> 

    

            android:layout_width="match_parent"
        android:layout_height="33dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:background="#ffffff"
        android:orientation="horizontal"
        android:id="@+id/linearLayout2">

                    android:id="@+id/pwd_lab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="20dp"
            android:text="密码:"
            android:textSize="18dp" />

                    android:id="@+id/pwd_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:hint="请输入密码"
            android:textSize="14sp"
            android:background="@null"
            android:ems="10"
            android:inputType="textPassword"
            android:imeOptions="actionDone"/> 
    

    

Java源代码:

package com.example.txjju.sybcase2_ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Activity中必须要重写一个onCreate方法
 * 并且要在onCreate这个方法中要为本页面配置一个布局xml文件
 */

public class login_activitys extends AppCompatActivity implements View.OnClickListener, TextView.OnEditorActionListener {
    private EditText nameEdit,pwdEdit;
    private Button btnLogin;
    private AlertDialog dialogLogin;
    private static final String LOG_TEST = "login_activitys";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitys_login);//配置res/layOut文件夹下的xml布局,使用R引用id
        init();
    }

    private void init() {
        nameEdit = findViewById(R.id.name_edit);
        pwdEdit = findViewById(R.id.pwd_edit);
        btnLogin = findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);//按钮的监听点击事件
        //监听回车
        // 需要注意的是 setOnEditorActionListener这个方法,并不是在我们点击EditText的时候触发,也不是在我们对EditText进行编辑时触发,
        // 而是在我们编辑完之后点击软键盘上的各种键才会触发。
        nameEdit.setOnEditorActionListener(this);
        pwdEdit.setOnEditorActionListener(this);

    }

    @Override
    public void onClick(View view) {
        String name = nameEdit.getText().toString();
        String pwd = pwdEdit.getText().toString();
        Log.i(LOG_TEST,"账户:"+name+";密码:"+pwd);
        Log.i(LOG_TEST,name.length()+"|"+pwd.length());
        if(name.length() == 0 || pwd.length() == 0){
            dialogLogin = new AlertDialog.Builder(this)
                    .setTitle("提示信息")
                    .setMessage("账户或密码未输入")
                    .setIcon(R.mipmap.head)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogLogin.hide();
                        }
                    }).create();
            dialogLogin.show();
        }else{
            Toast.makeText(this, "账户:"+name+"\n密码:"+pwd, Toast.LENGTH_LONG).show();
        }
    }

    /*
    * EditText是TextView子类
    * 方法:onEditorAction()
    * 第一个参数:TextView textView 表示当前触发事件的EditText的对象,类似于textView=findViewById(R.id.name_edit)
    * 第二个参数:int actionId 表示 按下“完成按钮”,这里和xml文件中EditText属性imeOptions对应
    *             但要注意actionId是指软盘上的,而键盘上的actionId与软盘上的不一样;即在软盘上“完成按钮”的actionId为0,而键盘上的“完成按钮(回车键)”的actionId为6
    * 第三个参数:KeyEvent keyEvent 表示 按下“完成按钮”,这里和xml文件中EditText属性imeOptions对应,但keyEvent里面的内容更丰富,内容如下:
    *             keyEvent.toString()=KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ENTER, scanCode=28, metaState=0, flags=0x8, repeatCount=0, eventTime=5664212, downTime=5664212, deviceId=1, source=0x301 }
    * 返回值:返回true,保留软键盘;false,隐藏软键盘
    */
    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        Toast.makeText(login_activitys.this, textView.getText().toString(), Toast.LENGTH_LONG).show();
        Log.i(LOG_TEST,actionId+"|"+EditorInfo.IME_ACTION_DONE);
        if (actionId == EditorInfo.IME_ACTION_DONE) {   // 按下完成按钮,这里和xml文件中的EditText中属性imeOptions对应;
            Toast.makeText(login_activitys.this, "测试成功了!", Toast.LENGTH_LONG).show();
            return false;   //返回true,保留软键盘;false,隐藏软键盘
        }
        return false;
    }
}

小包的登录界面不太好看,截图就免了啊,但大概的形状已经出来了哟!

上述实例中除了有对软键盘的监听事件,还有弹出框AlertDialog,提示信息框Toast,仅供参考!!!


你可能感兴趣的:(Android Studio 软键盘的监听事件setOnEditorActionListener)