Android用软键盘将整个界面推上去

[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]
<?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);

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值