首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

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

2012-07-18 
Android用软键盘将整个界面推上去在Android UI中,我们常常会使用EditText,当用户点击这个EditText时会触发

Android用软键盘将整个界面推上去
        在Android UI中,我们常常会使用EditText,当用户点击这个EditText时会触发软键盘,这个软键盘会把EditText以下的界面挡住,有时候我们希望用户看到完整的界面,就像下图这样:
   


        原理很简单,将布局的最外层添加一个ScrollView,当用户点击EditText时,将ScrollView滚动到底,废话不说,直接上代码
AndroidMainfest.xml

<?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>


        在布局页面中,我们在最外层使用了ScrollView,由于软键盘的弹出,原有界面从新布局,使得用户可以滚动界面。
main.xml

<?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>


下面是代码的逻辑部分,比较简单
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);    }    @Overridepublic void onClick(View v) {//这里必须要给一个延迟,如果不加延迟则没有效果。我现在还没想明白为什么mHandler.postDelayed(new Runnable() {@Overridepublic void run() {//将ScrollView滚动到底mScrollView.fullScroll(View.FOCUS_DOWN);}}, 100);}}

热点排行
Bad Request.