Android解决软键盘遮挡Button
来源:互联网 发布:软件代理加盟协议 编辑:程序博客网 时间:2024/06/05 11:51
今天来介绍软键盘遮挡住登录Button的方法,在登录和注册的时候界面最下方往往会有一个Button,但是用户在输入框中输入的时候软键盘很可能就会把一部分的输入框和Button遮挡了,在网上查了相关的资料解决的办法有很多种,但是都有不尽人意的地方(就我查到的相关解决方案),最后自己总结出了一个我觉得还可以的方案,现在分享给大家。直接上代码,里面主要部分都已经注释了。
AndroidManifest.xml
<activity android:name=".MainActivity" android:label="@string/app_name" android:windowSoftInputMode="stateHidden" >
android:windowSoftInputMode=”stateHidden”这个主要是在进入activity阻止自动弹出软键盘。
layout.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroll" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="输入框1" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入框2" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入框3" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入框4" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入框5" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入框6" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入框7" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入框8" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入框9" /> <EditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入框10" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="button" /> </LinearLayout></ScrollView>
布局中主要的是ScrollView,这样当软键盘显示的时候可以通过ScrollView来滑动界面。
MainActivity
public class MainActivity extends Activity implements OnTouchListener { private ScrollView scrollView; private EditText editText; private Handler handler; private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { editText = (EditText) findViewById(R.id.edittext); scrollView = (ScrollView) findViewById(R.id.scroll); layout = (LinearLayout) findViewById(R.id.layout); handler = new Handler(); editText.setOnTouchListener(this); layout.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.edittext: handler.postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub //让scrollview划到最下方 scrollView.fullScroll(ScrollView.FOCUS_DOWN); } }, 100); break; case R.id.layout: //点击空白处软键盘隐藏 InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); return imm.hideSoftInputFromWindow(getCurrentFocus() .getWindowToken(), 0); default: break; } return false; }}
在用户点击最下方的edittext的时候ScrollView就会划到最下方,这样就能把Button完全显示出来了。OK到此为止,已经完全解决了软键盘遮挡Button。
0 0
- Android解决软键盘遮挡Button
- 【Android】解决软键盘遮挡登陆按钮
- 【Android】解决软键盘遮挡登陆按钮
- android 解决软键盘遮挡popwindow问题
- Android 软键盘遮挡
- 解决软键盘遮挡按钮
- Android 模仿QQ登录界面解决软键盘遮挡问题
- Android解决被软键盘遮挡的EditText问题
- Android 全屏模式下,解决 软键盘遮挡
- Android软键盘遮挡问题
- Android软键盘遮挡问题解决
- android 软键盘遮挡布局
- Android 软键盘遮挡Dialog
- 完美解决 手机软键盘遮挡问题
- 完美解决 手机软键盘遮挡问题
- 记录资源 解决软键盘遮挡问题
- 解决软键盘遮挡登录按钮
- Android 软键盘遮挡控件的解决办法
- C和指针 (学习之旅)
- CodeVS1428 棋盘制作
- Scala深入浅出进阶经典 第56讲:Scala中Self Types实战详解
- Struts2 用通配符方式动态调用方法
- mybatis的一级缓存实现详解
- Android解决软键盘遮挡Button
- Android执行shell脚本命令(备忘记录与实际应用)
- 2015年华为面试经历
- JavaScript高级程序设计之基本概念之变量第3.3讲笔记
- 用简单的方法实战演练搭建http服务器(Windows上)------专为http新手
- Scala深入浅出进阶经典 第57讲:Scala中Dependency Injection实战详解
- C/C++ 中的次方运算
- 【Android杂谈】AsyncTask的详细使用解析
- linux之sed用法