editview软键盘弹出和隐藏
来源:互联网 发布:数据库集群的好处 编辑:程序博客网 时间:2024/06/01 09:46
需求:打开界面,editview自动弹出。点击软键盘其它区域,则收起软键盘。
先上图:界面效果图如下:
比较简单:直接上代码:
package com.testedittextdemo;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.view.MotionEvent;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;public class MainActivity extends Activity {EditText et_username;EditText et_password;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_username = (EditText) findViewById(R.id.et_username);et_password = (EditText) findViewById(R.id.et_userpassword);//延时1s,的目的是等待view加载完成后,才能够弹出软键盘。否则,软键盘可能弹出失败。new Handler().postDelayed(new Runnable() {@Overridepublic void run() {//打开软键盘InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);manager.showSoftInput(et_username, 0);}}, 1000);}//监听onTouchEvent事件,关闭软键盘。//getWindow().getDecorView()的意思是获取window的最前面的view。软键盘是phonewindow的跟view@Overridepublic boolean onTouchEvent(MotionEvent event) {//关闭软键盘InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);manager.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);return super.onTouchEvent(event);}}
xml的文件布局为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="1" android:text="登录名:" /> <EditText android:id="@+id/et_username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="1" android:text="用户密码:" /> <EditText android:id="@+id/et_userpassword" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:inputType="textPassword" /> </LinearLayout></LinearLayout>
源码下载地址:点击打开链接
0 0
- editview软键盘弹出和隐藏
- EditView 展示和隐藏软键盘
- EditView显示和隐藏软键盘(黑屏问题)
- EditView 阻止软键盘自动弹出
- EditView 阻止软键盘自动弹出
- android 下软键盘隐藏和弹出
- android自动弹出和隐藏软键盘
- 监听软键盘的隐藏和弹出
- Android软键盘弹出和隐藏
- Android-软键盘弹出和隐藏
- IOS软键盘弹出和隐藏
- Android软键盘的弹出和隐藏
- 软键盘隐藏和弹出的监听
- 软键盘弹出/隐藏
- Android之EditView 软键盘-自动弹出-禁止自动弹出
- android 关于软键盘的显示和隐藏监听,解决挡住Editview的问题
- Android中的EditView如何不默认获取焦点弹出软键盘,点击EditView后会弹出?
- Android之EditView 阻止软键盘自动弹出
- java环境变量配置
- linux常用命令
- datagrid表格数据单机选中与取消问题
- 用循环单链表的方式实现对冗余数据包的过滤处理
- 常用API接口
- editview软键盘弹出和隐藏
- Java中可变长参数的使用及注意事项
- 常用shell例子
- spring 动态获得bean实例
- 实现点击图片下标切换图片
- 【CTO讲堂】浅析工业级物联网项目的快速开发
- 事件的接受与忽略 accept() ignore() protected成员
- (PHP)用cURL调用WebService获取天气信息
- C#播放音乐,调用程序