android:如何做到软件盘弹出只顶起输入框

来源:互联网 发布:阿里云域名查询 编辑:程序博客网 时间:2024/04/30 09:23

最近在做视频直播项目中遇到这个问题,当用户点击主播房间,进行直播观看,在下方点击输入框发送信息时,发现了不光是EdiTextView 被软键盘弹起,就连surfaceView也被压扁了。哦,忘记说一点我在Manifest.xml定义该Activity的属性中添加的是WindowSoftInputMode="adjustResize"。

首先说一下两种WindowSoftInputMode的两个属性值

1. adjustResize的意思就是:Activity主窗口总是被调整屏幕的大小以便留出软键盘的空间,图示如下:


2. adjustPan 属性值意思:Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互操作。那么它的表示图如下:


ok !上面两个属性值介绍完毕,进入今天主题:我们主要是想达到如下效果:输入框被软键盘弹起,但是后面的SurfaceView并不被压缩变形。如图:

                          

(1)Xml 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.jinshanyundemo.SeeLiveActivity">    <com.example.jinshanyundemo.view.LockableScrollView        android:id="@+id/scrollView"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scrollbars="none">        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="wrap_content">            <SurfaceView                android:id="@+id/player_surface"                android:layout_width="match_parent"                android:layout_height="wrap_content" />        </RelativeLayout>    </com.example.jinshanyundemo.view.LockableScrollView>    <EditText        android:id="@+id/ed_mag"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:ems="10"        android:text="123"        android:textColor="#0ff000" />    <Button        android:id="@+id/bt_send"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_toRightOf="@+id/ed_mag"        android:text="发送" />    <Button        android:id="@+id/bt_car"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_toRightOf="@+id/bt_send"        android:text="发送车" /></RelativeLayout>

需要注意的几点:

1 .根布局是用RelativeLayout,这是必须的,因为只有这样控件和控件之间才可以覆盖。

2.SurfaView 必须使用ScrollView 包裹


LockScrollView 是一个自定义的控件主要就是对ScrollView的事件传递做一些处理,让ScrollView不能够滑动。具体代码下面会贴出

(2)manifest.xml中设置 Activity的属性如下:

  <activity            android:name=".SeeLiveActivity"            android:label="@string/title_activity_see_live"            android:windowSoftInputMode="adjustResize" />
我们使用的是adjustResize这个属性值。

(3)接下来Activity的代码:

 @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_see_live);        mScrollView = (LockableScrollView) findViewById(R.id.scrollView);        mScrollView.setScrollingEnabled(false);        surface = (SurfaceView) this.findViewById(R.id.player_surface);        <span style="color:#cc0000;">RelativeLayout</span>.LayoutParams lp = (RelativeLayout.LayoutParams) player_surface.getLayoutParams();        lp.height = getWindowManager().getDefaultDisplay().getHeight();        lp.width = getWindowManager().getDefaultDisplay().getWidth();        surface.setLayoutParams(lp);    }
主要控件的初始化也就是个SurfaceView,在这我根据屏幕的大小动态的设置了SurfaceView的尺寸。(全屏直播嘛

(4)最后就是自定义控件LockScrollView 

public class LockableScrollView extends ScrollView {    //是否允许滑动    private boolean mScrollable = true;    public LockableScrollView(Context context) {        super(context);    }    public LockableScrollView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public LockableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    public void setScrollingEnabled(boolean enabled) {        mScrollable = enabled;    }    public boolean isScrollable() {        return mScrollable;    }        //如果我们不需要滑动事件的话,直接返回false,不需要ScrollView做任何处理    @Override    public boolean onTouchEvent(MotionEvent ev) {        switch (ev.getAction()) {            case MotionEvent.ACTION_DOWN:                // if we can scroll pass the event to the superclass                if (mScrollable) return super.onTouchEvent(ev);                // only continue to handle the touch event if scrolling enabled                return mScrollable; // mScrollable is always false at this point            default:                return super.onTouchEvent(ev);        }    }        @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        // Don't do anything with intercepted touch events if        // we are not scrollable        if (!mScrollable) return false;        else return super.onInterceptTouchEvent(ev);    }}
代码很简单,就不说了,就是一个事件处理。

OK,到此问题就解决了。同志们在使用的时候可以将SurfaView 替换成ImageView 进行测试。


0 0
原创粉丝点击