Android用软键盘将整个界面推上去解决方案

来源:互联网 发布:淘宝小号挂机赚钱 编辑:程序博客网 时间:2024/04/29 13:28

     http://ipjmc.iteye.com/blog/1439657#comments

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

     


        原理很简单,将布局的最外层添加一个ScrollView,当用户点击EditText时,将ScrollView滚动到底,废话不说,直接上代码 
AndroidMainfest.xml
 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.ipjmc.demo.inputmethod"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:label="@string/app_name"  
  14.             android:name=".InputMethodActivity"  
  15.               
  16.             <!--只有用户点击了编辑框才显示软键盘,并且会导致原有界面重新布局-->  
  17.             android:windowSoftInputMode="stateHidden|adjustResize" >  
  18.             <intent-filter >  
  19.                 <action android:name="android.intent.action.MAIN" />  
  20.   
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.     </application>  
  25. </manifest>  


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

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView android:id="@+id/scroll" xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent" android:layout_height="match_parent"  
  4.     android:fillViewport="true" >  
  5.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="fill_parent"  
  8.         android:orientation="vertical" >  
  9.       
  10.         <TextView  android:text="@string/hello" android:textColor="#000"  
  11.             android:layout_width="fill_parent" android:layout_height="300dip"  
  12.             android:background="#0000FF" />  
  13.       
  14.         <EditText android:id="@+id/edit"  
  15.             android:layout_width="match_parent" android:layout_height="wrap_content"/>  
  16.           
  17.         <Button android:id="@+id/button" android:text="提交"  
  18.             android:layout_width="match_parent" android:layout_height="wrap_content"/>  
  19.     </LinearLayout>  
  20. </ScrollView>  


下面是代码的逻辑部分,比较简单 
Java代码  收藏代码
  1. package com.ipjmc.demo.inputmethod;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.ScrollView;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. public class InputMethodActivity extends Activity implements View.OnClickListener {  
  12.       
  13.     private static final String TAG = "Scroll";  
  14.     private EditText mEdit;  
  15.     private Button mButton;  
  16.     private ScrollView mScrollView;  
  17.     private Handler mHandler = new Handler();  
  18.     /** Called when the activity is first created. */  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.           
  24.         mScrollView = (ScrollView) findViewById(R.id.scroll);  
  25.         mEdit = (EditText) findViewById(R.id.edit);  
  26.         mButton = (Button) findViewById(R.id.button);  
  27.           
  28.         mEdit.setOnClickListener(this);  
  29.     }  
  30.       
  31.     @Override  
  32.     public void onClick(View v) {  
  33.           
  34.         //这里必须要给一个延迟,如果不加延迟则没有效果。我现在还没想明白为什么  
  35.         mHandler.postDelayed(new Runnable() {  
  36.               
  37.             @Override  
  38.             public void run() {  
  39.                 //将ScrollView滚动到底  
  40.                 mScrollView.fullScroll(View.FOCUS_DOWN);  
  41.             }  
  42.         }, 100);  
  43.           
  44.     }  
  45. }  


至于为什么要用Handler来延迟的问题,答案是:http://blog.csdn.net/t12x3456/article/details/12799825

因为Android很多函数都是基于消息队列来同步,所以需要异步操作,
addView完之后,不等于马上就会显示,而是在队列中等待处理,虽然很快,但是如果立即调用fullScroll, view可能还没有显示出来,所以会失败,应该通过handler在新线程中更新
0 0
原创粉丝点击