Android设置输入框和软键盘动态悬浮
2016-09-29 17:11
447 查看
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.liuzheng.admin.myhidden.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:id="@+id/butt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="true" android:focusableInTouchMode="true" android:text="显示" /> <Button android:id="@+id/butt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐藏" /> <EditText android:id="@+id/edit_text" android:layout_width="200dp" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>
public class MainActivity extends AppCompatActivity { private Button butt1; private Button butt2; private EditText edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit = (EditText) findViewById(R.id.edit_text); butt1 = (Button) findViewById(R.id.butt1); butt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //绑定软键盘到EditText edit.setFocusable(true); edit.setFocusableInTouchMode(true); edit.requestFocus(); InputMethodManager inputManager = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(edit, 0); } }); butt2 = (Button) findViewById(R.id.butt2); butt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 去除软键盘显示 edit.clearFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(), 0); } }); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.liuzheng.admin.myhidden"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>在 项目的AndroidManifest.xml文件中界面对应的<activity>里加入
android:windowSoftInputMode="adjustResize"
各值的含义: 【A】stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 【B】stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示 【C】stateHidden:用户选择activity时,软键盘总是被隐藏 【D】stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的 【E】stateVisible:软键盘通常是可见的 【F】stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态 【G】adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示 【H】adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间 【I】adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分
相关文章推荐
- Android Cannot merge new index 67208 into a non-jumbo instruction
- android性能优化
- Android中的性能优化问题
- Android Java.lang.UnsatisfiedLinkError: Couldn't load locSDK3: findLibrary returned null.
- Android Fragment中嵌套Fragment,不显示view
- Android通过webservice连接SQLServer 详细教程(数据库+服务器+客户端)
- Android GPS定位详解及实例代码
- Android中的“再按一次返回键退出程序”实现
- Android开发需要知道的几个开源库
- 解决方法数超出 android优化 清除无效代码 UCDetector
- Android.mk详解
- Android 无线调试
- android单选按钮(RadioButton)、复选框(CheckBox)
- Android Studio目录结构浅析!!!
- 悬浮窗错误android.view.WindowManager$BadTokenException: permission denied for this window type
- Android 的透明色的代码
- Android的活动常见的状态
- 值得收藏的Android开发文集
- 事件分发
- Android调取系统相册和相机照片设置到ImageView并上传到服务器