[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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ipjmc.demo.inputmethod"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".InputMethodActivity"
<!--只有用户点击了编辑框才显示软键盘,并且会导致原有界面重新布局-->
android:windowSoftInputMode="stateHidden|adjustResize" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
[size=medium] 在布局页面中,我们在最外层使用了ScrollView,由于软键盘的弹出,原有界面从新布局,使得用户可以滚动界面。
main.xml[/size]
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/scroll" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:text="@string/hello" android:textColor="#000"
android:layout_width="fill_parent" android:layout_height="300dip"
android:background="#0000FF" />
<EditText android:id="@+id/edit"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/button" android:text="提交"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
[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);
}
}