如何将编辑框EditText置于系统软键盘之上及相关细节的实现
来源:互联网 发布:实时荧光定量pcr数据分析 编辑:程序博客网 时间:2024/06/06 12:34
转载请注明出处: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" > <Button android: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" /> <Button android: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-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android: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
0 0
- 如何将编辑框EditText置于系统软键盘之上及相关细节的实现
- Edittext编辑软键盘
- edittext的相关设置及软键盘的设置
- 将显示对象置于PopUpManager创建的显示容器之上
- 将spin控件置于编辑框内
- Android EditText弹出软键盘实现页面标题头不动,软键盘弹出在编辑框下面
- Android EditText弹出软键盘实现页面标题头不动,软键盘弹出在编辑框下面
- 将层Div置于Flash之上
- android怎样将textview置于imageview之上
- 将DIV层置于Flash之上
- Android如何将软键盘回车换成搜索等按钮,EditText中imeOptions属性的使用
- 复杂布局中点击编辑框EditText以外界面无法隐藏软键盘的解决方法
- 如何将TabHost置于屏幕的下方
- Android 软键盘及EditText
- EditText 去光标和下划线,控件置于另一控件之上
- Android开发EditText弹出软键盘的相关使用
- android PopupWindow消失后,将EditText的软键盘隐藏
- android PopupWindow消失后,将EditText的软键盘隐藏
- Android RatingBar自定义替换系统图片
- IOS frame与bounds的区别
- halcon学习 tuple之算术运算3
- Calendar年历中周的问题
- 论文提要“Selective Search for Object Recognition”
- 如何将编辑框EditText置于系统软键盘之上及相关细节的实现
- SPDY 是什么?如何部署 SPDY?
- ubuntu 创建新用户问题
- 微信企业号开发:微信考勤百度地图定位
- Linux vmstat命令实战详解
- 【more effective c++读书笔记】【第5章】技术(7)——让函数根据一个以上的对象类型来决定如何虚化(2)
- 分析(Android下的任务和Activity栈)
- StringBuilder 和 StringBuffer 区别
- 黑马程序员_java06_泛型