如何将编辑框EditText置于系统软键盘之上及相关细节的实现

本文主要是介绍如何将编辑框EditText置于系统软键盘之上及相关细节的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载请注明出处:http://blog.csdn.net/u010214991/article/details/48345437

最近在做类似于QQ空间和微信朋友圈的评论界面,顺便研究了下Android软键盘的一些使用方法,在网上收集了大量资料后自己进行了一些总结,并且通过亲测可用,希望能给广大读者带来用处。好了下面开始正题。



为了让大家更容易看懂,我贴出了一个小例子,在这个例子中有主要的活动界面MainActivity,有主要用于监听软键盘显示与隐藏的KeyboardListenRelativeLayout,在这里值得提出的是,由于软键盘并没有显示或隐藏的监听事件,因此我们只能通过判断当前布局的高度变化来进行软键盘的显示与隐藏(软键盘在弹出或收起时所在布局高度会改变)。而为了能够让整个界面能够自动调整高度,我们需要在配置的当前MainActivity中添加:android:windowSoftInputMode="adjustResize|stateHidden"。下面贴代码:




1.MainActivity.class

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;public class MainActivity extends Activity implements OnClickListener {private Button button;private LinearLayout ll;InputMethodManager imm;private KeyboardListenRelativeLayout relativeLayout;private int oldHeight;private View view;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button1);ll = (LinearLayout) findViewById(R.id.line);relativeLayout = (KeyboardListenRelativeLayout) findViewById(R.id.mainlayout);view = findViewById(R.id.xian);imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);button.setOnClickListener(this);relativeLayout.setOnKeyboardStateChangedListener(new KeyboardListenRelativeLayout.IOnKeyboardStateChangedListener() {@Overridepublic void onKeyboardStateChanged(int state) {// TODO Auto-generated method stubswitch (state) {case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE:ll.setVisibility(View.GONE);view.setVisibility(View.GONE);break;default:break;}}});}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubimm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);ll.requestFocus();ll.setVisibility(View.VISIBLE);view.setVisibility(View.VISIBLE);}@Overridepublic boolean onTouchEvent(MotionEvent event) {//该方法用于点击编辑框以外画面时隐藏软键盘,适用于简单的布局,复杂的可能无效// TODO Auto-generated method stubif (getCurrentFocus() != null&& getCurrentFocus().getWindowToken() != null) {imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);//隐藏软键盘}return super.onTouchEvent(event);}}





2.KeyboardListenRelativeLayout.class

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;public class KeyboardListenRelativeLayout extends RelativeLayout {private static final String TAG = KeyboardListenRelativeLayout.class.getSimpleName();  public static final byte KEYBOARD_STATE_SHOW = -3;  public static final byte KEYBOARD_STATE_HIDE = -2;  public static final byte KEYBOARD_STATE_INIT = -1;  private boolean mHasInit = false;  private boolean mHasKeyboard = false;  private int mHeight;  private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;  public KeyboardListenRelativeLayout(Context context) {  super(context);  }  public KeyboardListenRelativeLayout(Context context, AttributeSet attrs) {  super(context, attrs);  }  public KeyboardListenRelativeLayout(Context context, AttributeSet attrs, int defStyle) {  super(context, attrs, defStyle);  }  public void setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) {  this.onKeyboardStateChangedListener = onKeyboardStateChangedListener;  }  @Override  protected void onLayout(boolean changed, int l, int t, int r, int b) {  super.onLayout(changed, l, t, r, b);  if(!mHasInit) {  mHasInit = true;  mHeight = b;  if(onKeyboardStateChangedListener != null) {  onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_INIT);  }  } else {  mHeight = mHeight < b ? b : mHeight;  }  if(mHasInit && mHeight > b) {  mHasKeyboard = true;  if(onKeyboardStateChangedListener != null) {  onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_SHOW);  }  }  if(mHasInit && mHasKeyboard && mHeight == b) {  mHasKeyboard = false;  if(onKeyboardStateChangedListener != null) {  onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_HIDE);  }  }  }  public interface IOnKeyboardStateChangedListener {  public void onKeyboardStateChanged(int state);  }  
}




3.MainActivity的布局文件(activity_main.xml)

<com.example.test.KeyboardListenRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/mainlayout"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:layout_marginRight="101dp"android:layout_marginTop="75dp"android:text="发表" /><LinearLayout android:id="@+id/line"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_alignParentBottom="true"android:gravity="center_vertical"android:visibility="gone"><EditText android:id="@+id/edit"android:layout_width="wrap_content"android:layout_height="match_parent"android:hint="说点什么吧..."android:background="@null"android:layout_weight="10"/><Buttonandroid:layout_width="70dp"android:layout_height="42dp"android:background="#cfcad2"android:text="发送" /></LinearLayout><View android:id="@+id/xian"android:layout_width="match_parent"android:layout_height="0.8dp"android:layout_alignTop="@id/line"android:background="#cfcad2"android:visibility="gone"/>
</com.example.test.KeyboardListenRelativeLayout>



4.配置文件,其实就只是在当前活动中添加android:windowSoftInputMode="adjustResize|stateHidden"这句而已

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.test"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="18" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.test.MainActivity"android:windowSoftInputMode="adjustResize|stateHidden"android:screenOrientation="portrait"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>


最后附上演示例子供下载:

将编辑框EditText置于系统软键盘之上Demo


这篇关于如何将编辑框EditText置于系统软键盘之上及相关细节的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/972395

相关文章

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

一文详解如何从零构建Spring Boot Starter并实现整合

《一文详解如何从零构建SpringBootStarter并实现整合》SpringBoot是一个开源的Java基础框架,用于创建独立、生产级的基于Spring框架的应用程序,:本文主要介绍如何从... 目录一、Spring Boot Starter的核心价值二、Starter项目创建全流程2.1 项目初始化(

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

mysql数据库重置表主键id的实现

《mysql数据库重置表主键id的实现》在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,本文主要介绍了mysql数据库重置表主键id的实现,具有一定的参考价值,感兴趣的可以了... 目录关键语法演示案例在我们的开发过程中,难免在做测试的时候会生成一些杂乱无章的SQL主键数据,当我们

SpringBoot配置Ollama实现本地部署DeepSeek

《SpringBoot配置Ollama实现本地部署DeepSeek》本文主要介绍了在本地环境中使用Ollama配置DeepSeek模型,并在IntelliJIDEA中创建一个Sprin... 目录前言详细步骤一、本地配置DeepSeek二、SpringBoot项目调用本地DeepSeek前言随着人工智能技

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

MySQL多列IN查询的实现

《MySQL多列IN查询的实现》多列IN查询是一种强大的筛选工具,它允许通过多字段组合快速过滤数据,本文主要介绍了MySQL多列IN查询的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析与优化1.