Android用软键盘将整个界面推上去
16lz
2021-01-26
[size=medium] 在Android UI中,我们常常会使用EditText,当用户点击这个EditText时会触发软键盘,这个软键盘会把EditText以下的界面挡住,有时候我们希望用户看到完整的界面,就像下图这样:
[img]http://dl.iteye.com/upload/attachment/0064/1583/43001536-53fb-3fb7-aa76-c2194c8a5bc6.png[/img] [img]http://dl.iteye.com/upload/attachment/0064/1585/4a165c44-352f-3f96-a6fa-be8682d63b5a.png[/img]
原理很简单,将布局的最外层添加一个ScrollView,当用户点击EditText时,将ScrollView滚动到底,废话不说,直接上代码
AndroidMainfest.xml[/size]
[size=medium] 在布局页面中,我们在最外层使用了ScrollView,由于软键盘的弹出,原有界面从新布局,使得用户可以滚动界面。
main.xml[/size]
[size=medium]下面是代码的逻辑部分,比较简单[/size]
[img]http://dl.iteye.com/upload/attachment/0064/1583/43001536-53fb-3fb7-aa76-c2194c8a5bc6.png[/img] [img]http://dl.iteye.com/upload/attachment/0064/1585/4a165c44-352f-3f96-a6fa-be8682d63b5a.png[/img]
原理很简单,将布局的最外层添加一个ScrollView,当用户点击EditText时,将ScrollView滚动到底,废话不说,直接上代码
AndroidMainfest.xml[/size]
<?xml version="1.0" encoding="utf-8"?>
package="com.ipjmc.demo.inputmethod"
android:versionCode="1"
android:versionName="1.0" >
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:label="@string/app_name"
android:name=".InputMethodActivity"
android:windowSoftInputMode="stateHidden|adjustResize" >
[size=medium] 在布局页面中,我们在最外层使用了ScrollView,由于软键盘的弹出,原有界面从新布局,使得用户可以滚动界面。
main.xml[/size]
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent" android:layout_height="match_parent"
android:fillViewport="true" >
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent" android:layout_height="300dip"
android:background="#0000FF" />
android:layout_width="match_parent" android:layout_height="wrap_content"/>
[size=medium]下面是代码的逻辑部分,比较简单[/size]
package com.ipjmc.demo.inputmethod;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.util.Log;
import android.view.View;
public class InputMethodActivity extends Activity implements View.OnClickListener {
private static final String TAG = "Scroll";
private EditText mEdit;
private Button mButton;
private ScrollView mScrollView;
private Handler mHandler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mScrollView = (ScrollView) findViewById(R.id.scroll);
mEdit = (EditText) findViewById(R.id.edit);
mButton = (Button) findViewById(R.id.button);
mEdit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//这里必须要给一个延迟,如果不加延迟则没有效果。我现在还没想明白为什么
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
//将ScrollView滚动到底
mScrollView.fullScroll(View.FOCUS_DOWN);
}
}, 100);
}
}
更多相关文章
- Android实现计算器布局(四种布局方式)之GridLayout网格布局
- 完美解决Android中AVD模拟器启动慢(卡在Android启动界面)等问题
- EditText软键盘弹出相关问题
- Android(安卓)输入法键盘遮挡控件问题的解决办法
- Android5.X之百分比布局库(percent-support-lib) 解析与扩展
- Android:控件布局(表格布局)TableLayout
- [转]android学习总结----Activity view
- LinearLayout源码分析
- 【Android(安卓)界面效果18】Android软件开发之常用系统控件界面