在项目中,多多少少会遇到修改软键盘右下角按钮的需求,虽然已经写过几次,但是还是觉得在这里专心做个笔记比较放心 ~

我的那些软键盘Blog ~

  • Android进阶之路 - 常见软键盘操作行为
  • Android进阶之路 - 软键盘中右下角的设置与监听
  • Android进阶之路 - 软键盘顶起解决方案
  • Android进阶之路 - 监听软键盘当前状态,实现布局上移

Catalog

  • xml - 部分实现
  • 功能 - 部分实现
  • 完整实现

Effect :


xml - 部分实现

  • 确定右下角的功能展示键盘
android:imeOptions="actionSearch"

补充:常见类型

类型 含义
actionGo Go
actionSearch 放大镜
actionSend Send
actionNext Next
actionDone 完成
  • 确定当前的EditText条目(自我认为,它的字面是独立一行)
android:singleLine="true"
  • 完整的单个EditText代码
   <EditTextandroid:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:hint="搜索"android:imeOptions="actionSearch"android:singleLine="true"/>

功能 - 部分实现

  • 设置动作监听,如:
mSearch.setOnEditorActionListener(this);
  • 进行动作监听处理
   @Overridepublic boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {boolean state = true;switch (actionId) {case EditorInfo.IME_ACTION_SEARCH:Toast.makeText(this, "软键盘内部 - 搜索", Toast.LENGTH_LONG).show();hintKeyboard();break;default:state = false;break;}return state;}
  • 隐藏键盘方法
    /*隐藏软键盘*/void hintKeyboard() {InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);if (inputMethodManager.isActive()) {inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);}}

完整实现

MainActivity :

package com.example.yongliu.keyboard;import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextView.OnEditorActionListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);EditText mSearch = findViewById(R.id.search);EditText mNext = findViewById(R.id.next);EditText mDone = findViewById(R.id.done);EditText mGo = findViewById(R.id.go);EditText mSend = findViewById(R.id.send);mSearch.setOnClickListener(this);mNext.setOnClickListener(this);mDone.setOnClickListener(this);mGo.setOnClickListener(this);mSend.setOnClickListener(this);mSearch.setOnEditorActionListener(this);mNext.setOnEditorActionListener(this);mDone.setOnEditorActionListener(this);mGo.setOnEditorActionListener(this);mSend.setOnEditorActionListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.search:Toast.makeText(this, "搜索 - 点击", Toast.LENGTH_LONG).show();break;case R.id.next:Toast.makeText(this, "下一步 - 点击", Toast.LENGTH_LONG).show();break;case R.id.done:Toast.makeText(this, "完成 - 点击", Toast.LENGTH_LONG).show();break;case R.id.go:Toast.makeText(this, "Go - 点击", Toast.LENGTH_LONG).show();break;case R.id.send:Toast.makeText(this, "发送 - 点击", Toast.LENGTH_LONG).show();break;}}@Overridepublic boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {boolean state = true;switch (actionId) {case EditorInfo.IME_ACTION_SEARCH:Toast.makeText(this, "软键盘内部 - 搜索", Toast.LENGTH_LONG).show();hintKeyboard();break;case EditorInfo.IME_ACTION_NEXT:Toast.makeText(this, "软键盘内部 - 下一步", Toast.LENGTH_LONG).show();hintKeyboard();break;case EditorInfo.IME_ACTION_DONE:Toast.makeText(this, "软键盘内部 - 完成", Toast.LENGTH_LONG).show();hintKeyboard();break;case EditorInfo.IME_ACTION_GO:Toast.makeText(this, "软键盘内部 - Go", Toast.LENGTH_LONG).show();hintKeyboard();break;case EditorInfo.IME_ACTION_SEND:Toast.makeText(this, "软键盘内部 - 发送", Toast.LENGTH_LONG).show();hintKeyboard();break;default:state = false;break;}return state;}/*隐藏软键盘*/void hintKeyboard() {InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);if (inputMethodManager.isActive()) {inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);}}
}

MainActivity Xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.yongliu.keyboard.MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="45dp"android:gravity="center"android:text="软键盘监听"/><EditTextandroid:id="@+id/search"android:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:gravity="center|start"android:hint="搜索"android:imeOptions="actionSearch"android:paddingLeft="15dp"android:singleLine="true"android:textSize="13sp"/><EditTextandroid:id="@+id/send"android:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:gravity="center|start"android:hint="发送"android:imeOptions="actionSend"android:paddingLeft="15dp"android:singleLine="true"android:textSize="13sp"/><EditTextandroid:id="@+id/go"android:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:gravity="center|start"android:hint="Go"android:imeOptions="actionGo"android:paddingLeft="15dp"android:singleLine="true"android:textSize="13sp"/><EditTextandroid:id="@+id/next"android:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:gravity="center|start"android:hint="下一步"android:imeOptions="actionNext"android:paddingLeft="15dp"android:singleLine="true"android:textSize="13sp"/><EditTextandroid:id="@+id/done"android:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:gravity="center|start"android:hint="完成"android:imeOptions="actionDone"android:paddingLeft="15dp"android:singleLine="true"android:textSize="13sp"/>
</LinearLayout>

Android进阶之路 - 软键盘中右下角的设置与监听相关推荐

  1. Android软键盘弹出和收起的监听

    Android软键盘弹出和收起的监听 1.直接调用 SoftKeyboardHelper softKeyboardHelper = softKeyboardHelper = new SoftKeybo ...

  2. Android开发 软键盘的右下角变为搜索按钮

    第一步 xml文件中添加Edittext控件 <EditTextandroid:id="@+id/et_key_word"android:layout_width=" ...

  3. Android进阶之路 - 解决部分手机拍照之后图片被旋转的问题

    这几天犯了一个错误,初期想着甩锅给后台的- 但还好及时发现了是自身的问题~ 关联文章 Android基础进阶 - 调用拍照.获取图片(基础) Android基础进阶 - 获取.调用相册内图片(基础) ...

  4. Android使用xml自定义软键盘效果(附源码)

    Android使用xml自定义软键盘效果原理: 1,软键盘其实是个控件,使用android.inputmethodserver.KeyboardView类定义. 2,主布局中使用帧布局,当我们需要显示 ...

  5. Android进阶之路 - 批量下载、缓存图片、视频

    之前已经记录过,批量下载图片和缓存本地的方式,此篇主要记录批量下载图片.视频,同时缓存在本地的功能实现 关联篇 Android进阶之路 - 批量下载.缓存图片 Android进阶之路 - 批量下载.缓 ...

  6. android+点击屏幕隐藏键盘,Android 显示和隐藏软键盘的方法(手动)

    在Android开发中,经常会有一个需求,做完某项操作后,隐藏键盘,也即让Android中的软键盘不显示.今天,和大家分享如何利用代码来实现对Android的软件盘的隐藏.显示的操作. 1.方法一(如 ...

  7. android隐藏软键盘方法,Android显示和隐藏软键盘方法

    InputMethodManager类 Android中软键盘的管理主要是通过InputMethodManager类来完成的. InputMethodManager对象的获取方法如下: 获取到Inpu ...

  8. Android进阶之路 - 批量下载、缓存图片

    在日常项目开发中,关于图片批量下载,数据缓存的相关功能比比皆是,这次也是去年在项目中需要在本地缓存商品数据,所以用到了批量下载的功能,特此记录 ~ 关联篇 Android进阶之路 - 批量下载.缓存图 ...

  9. Android进阶之路 - 存、取、读 本地 Json 文件

    最近在开发中又开始加载一些本地的json数据源,回头看之前竟然没记录,赶紧记录一波 ~ 如何准备一个合格的json文件? AndoridStudio中如何存放json文件? 如何读取本地Json文件数 ...

最新文章

  1. 在Dockerfile中设置G1垃圾回收器参数
  2. GPU加速库AmgX
  3. AI芯片重磅破局者,开启边缘新智元
  4. HTTP协议理解——计算机网络
  5. delphi函数,识别字符集编码
  6. Android 中的adapter和作用以及常见的adapter
  7. EM算法极其推广---EM算法的收敛性
  8. sql简介_SQL简介
  9. ng service(服务)
  10. 将数据库表中的数据读出以xml的形式下载到手机端
  11. UAC2.0 Requests处理
  12. Android:如何查看Android源码
  13. 如何将socket服务器部署到云服务器上
  14. 好架构师都是写代码写出来的
  15. linux socket 阻塞服务端 非阻塞客户端,Linux socket非阻塞connect方法
  16. 理解Spring 容器设计理念
  17. linux 自启动 快捷键,linux自定义快捷键、文件打开方式、文件快捷方式、启动器及开机启动...
  18. 不可不知!4种常见的黑客攻击手段
  19. 一寸等于多少厘米,一寸照片尺寸是多少?
  20. Python获取 当前目录、上一级目录

热门文章

  1. VS2017安装Newtonsoft.Json插件
  2. geoserver 3_【第255期】排列3研究分析(254期中直选组选大底253期中精选底中7码组选中直选组选底251期中直选组选大底...
  3. 首届中国CDO大调研,CDO神秘面纱即将揭开...
  4. 网络安全菜鸟学习之漏洞篇——文件包含漏洞
  5. 海军开幕式 礼炮问题
  6. 兄弟Brother PT-P710BT 驱动
  7. Skype和Lync可以互相语音聊天啦
  8. 英文文献翻译神器——SCITranslate
  9. rom 是计算机的__,蘑菇ROM助手
  10. print函数的使用