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 进行测试。
- android:如何做到软件盘弹出只顶起输入框
- Android dialog软键盘弹出顶起View
- 软件盘如何单独顶起Editview组件
- android 弹出软键盘将底部视图顶起问题
- android 弹出软键盘将底部视图顶起有关问题
- 关于android软键盘弹出将edittext顶起问题
- window.open 如何做到多次点击只弹出一个窗口
- Android防止键盘弹出后顶起布局
- Android防止键盘弹出后顶起布局
- Android 设置键盘弹出后顶起布局
- android技术--------如何解决弹出键盘只影响当前fragment输入的窗口
- EditText 软键盘弹出方式探究 以及手动解决浸入式布局软键盘不顶起输入框或者顶掉整个UI的问题
- 软键盘弹出时顶起上方布局
- Android开发底部控件会被弹出的软键盘顶起
- 安卓键盘顶起输入框
- android 输入顶起输入框下被隐藏布局的实现方法
- Android 软键盘弹出,遮住输入框
- C#如何弹出输入框
- 重启http服务
- synchronized(this)与synchronized(class)
- MySQL自定义函数用法详解-复合结构自定义变量/流程控制--及其相关案例
- 暑期至今学习总结
- 数据结构示例之顺序数组的实现
- android:如何做到软件盘弹出只顶起输入框
- Spark RDD
- 数据结构实验之数组三:快速转置
- google protobuf数据类型
- Java list 判断时需判断非空
- 【思想感悟】一屋不扫何以扫天下
- 数字和一个数学运算符
- 阶乘
- TTL(生存时间)介绍