软键盘使用
来源:互联网 发布:搜狗五笔输入法 mac 编辑:程序博客网 时间:2024/05/22 04:44
运行效果:
功能:
1、点击回复按钮弹出软键盘并带有(EditText)
2、点击回复区域其他地方软键盘收缩,并隐藏EditText
2、点击back按钮,隐藏软键盘和EditText
代码:
public class MainActivity extends Activity { private EditText etSend; private InputMethodManager imm; private MyLinearlayout layout1;//自定义的一个线性布局 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); setContentView(R.layout.activity_main); Button btTest = (Button) findViewById(R.id.btTest); etSend = (EditText) findViewById(R.id.etSend); layout1 = (MyLinearlayout) findViewById(R.id.layoutSend);//覆盖在主布局上的layout imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);//获得软键盘服务 //线性布局的点击事件 layout1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //点击隐藏 if (layout1.getVisibility() == View.VISIBLE) { layout1.setVisibility(View.GONE); } if (imm.isActive()) { imm.hideSoftInputFromWindow(view.getWindowToken(), 0);//隐藏软键盘 } } }); btTest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //点击回复按钮 弹出发送框 layout1.setVisibility(View.VISIBLE);//设置布局可见 etSend.requestFocus();//给EditText设置焦点 imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//弹出软键盘 } }); }}xml布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/btTest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="回复" /> </LinearLayout> <com.zuoni.keyboarddemo.MyLinearlayout android:id="@+id/layoutSend" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:visibility="gone"> <LinearLayout android:id="@+id/layout1111" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" /> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:orientation="horizontal"> <EditText android:id="@+id/etSend" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" android:background="#c2c1c1" /> <Button android:layout_width="wrap_content" android:layout_height="45dp" android:text="发送" /> </LinearLayout> </com.zuoni.keyboarddemo.MyLinearlayout></FrameLayout>自定义的线性布局:
public class MyLinearlayout extends LinearLayout { public MyLinearlayout(Context context) { super(context); } public MyLinearlayout(Context context, AttributeSet attrs) { super(context, attrs); } public MyLinearlayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } //重写back事件 @Override public boolean dispatchKeyEventPreIme(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (MyLinearlayout.this.getVisibility() == VISIBLE) { MyLinearlayout.this.setVisibility(GONE); } } return super.dispatchKeyEventPreIme(event); }}实现原理:
1.在原来布局上方覆盖一层带有回复按钮的布局(使用FrameLayout)并设置为隐藏
2.点击回复按钮时候,将覆盖的布局显示出来
3.给“覆盖布局”设置点击事件,点击就隐藏自己并收回软键盘
为什么要自定义线性布局:
由于按back的时候,命令先被软键盘所拦截了,所以不能直接在MainActivity中拦截back事件
因此自定义线性布局,重写dispatchKeyEventPreIme 方法这样就可以按back键的时候让软键盘和布局同时接收事件(执行隐藏代码)
关键点:
要在set ContentView之前设置:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
否则会出现EditText下方部分会被软键盘覆盖
0 0
- 系统软键盘使用
- EditText使用软键盘
- 软键盘使用
- C#中使用软键盘
- android软键盘的使用
- 使用API方式调用软键盘
- 使用KeyBoardUtil处理软键盘的弹出
- 使用KeyBoardUtil处理软键盘的弹出
- 板子上移植使用软键盘
- IOS 软键盘灵活使用KeyBoard库
- 使用KeyBoardUtil处理软键盘的弹出
- Android自定义软键盘KeyboardView 使用实例
- android 软键盘 EditText 使用总结
- Andriod软键盘的使用总结
- android 使用软键盘的一些技巧
- android:windowSoftInputMode 软键盘属性使用
- Android中软键盘使用总结(一)
- 使用MUI 软键盘弹起挤压页面
- HTTP协议中几个状态码的含义:1xx(临时响应)
- Windows10下配置numpy&scipy
- C++中memset函数的用法
- include和require的区别
- LeetCode 401. Binary Watch 解题报告
- 软键盘使用
- LU分解Matlab算法分析
- 国内渗透测试新神器--北极熊扫描器4.0
- 懒加载过滤器
- 机器学习资料
- #ifdef _cplusplus
- Patrol Robot 巡逻的机器人 UVA
- 【JavaWeb-17】国际化、自定义拦截器、struts2文件上传与下载、OGNL、contextMap
- jquery eval解析JSON中的注意点介绍