ScrollView内嵌套EditText导致EditText无法滑动

来源:互联网 发布:网易闪电邮网络超时 编辑:程序博客网 时间:2024/06/05 08:36

最近也碰到这样的问题  搜索了网上一个答案  解决方法有两种,这个里面完成是照着别人的思路来,


第一种思路就是  把 EditText 放在 ScrollView里面,重新自定义一个ScrollView  

 那个代码不是很方便,我感觉他的自定义Scrollview里面的onInterceptTouchEvent这个方法不好,导致我们需要在实现的类中还需要去构造这样的ScrollView   不是很方面,

<strong><span style="font-size:18px;"><ScrollView         android:id="@+id/sv_scrollview"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:fadingEdge="none"         android:scrollbars="none" >    <LinearLayout             android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:orientation="vertical" >      <cn.ls.widget.ScrollviewEdit                 android:id="@+id/sv_feedback"                 android:layout_width="fill_parent"                 android:layout_height="100dip"                 android:layout_gravity="center"                 android:fadingEdge="none"                 android:scrollbars="none"                 android:visibility="visible" >                   <LinearLayout                     android:layout_width="fill_parent"                     android:layout_height="100dip"                     android:gravity="center"                     android:scrollbars="vertical" >                       <EditText                         android:id="@+id/et_feedback"                         android:layout_width="fill_parent"                         android:layout_height="wrap_content"                         android:layout_gravity="center"                         android:layout_marginLeft="13dip"                         android:layout_marginRight="13dip"                         android:clickable="true"                         android:enabled="false"                         android:focusable="false"                         android:gravity="top"                         android:scrollbars="vertical"                         android:singleLine="false"                         android:background="@null"                         android:text="" />                                       </LinearLayout>             </cn.ls.widget.ScrollviewEdit>        </LinearLayout>     </ScrollView>  </span></strong>

自定义的scrollview

<strong><span style="font-size:18px;">public class ScrollviewEdit extends ScrollView {        private static final String TAG = "ScrollviewEdit";         private ScrollView parent_scrollview;       public ScrollView getParent_scrollview() {          return parent_scrollview;      }      public void setParent_scrollview(ScrollView parent_scrollview) {          this.parent_scrollview = parent_scrollview;      }        public ScrollviewEdit(Context context) {          super(context);      }            public ScrollviewEdit(Context context, AttributeSet attrs) {          super(context, attrs);      }        int currentY;      @Override      public boolean onInterceptTouchEvent(MotionEvent ev) {          LogUtils.i(TAG, "onInterceptTouchEvent--------");              if (parent_scrollview == null) {              return super.onInterceptTouchEvent(ev);          } else {              if (ev.getAction() == MotionEvent.ACTION_DOWN) {                  // 将父scrollview的滚动事件拦截                  currentY = (int) ev.getY();                  setParentScrollAble(false);                  LogUtils.i(TAG, "将父scrollview的滚动事件拦截-----");                      return super.onInterceptTouchEvent(ev);                } else if (ev.getAction() == MotionEvent.ACTION_UP) {                  // 把滚动事件恢复给父Scrollview                  setParentScrollAble(true);                  LogUtils.i(TAG, "把滚动事件恢复给父Scrollview-----");                } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {              }          }          return super.onInterceptTouchEvent(ev);        }      /**      * 是否把滚动事件交给父scrollview      * @param flag      */      private void setParentScrollAble(boolean flag) {          parent_scrollview.requestDisallowInterceptTouchEvent(!flag);      }  }  </span></strong>

连接地址:http://blog.csdn.net/yigelangmandeshiren/article/details/12168877

第二种思路就是让 EditText重新获取焦点,让父滚动的焦点暂时把权利叫给自己,

<strong><span style="font-size:18px;">import android.content.Context;import android.graphics.PointF;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.EditText;public class XWEditText extends EditText{private XWEditText mthis;public XWEditText(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);mthis = this;// TODO Auto-generated constructor stub}public XWEditText(Context context, AttributeSet attrs) {super(context, attrs);mthis = this;// TODO Auto-generated constructor stub}//private PointF cpublic XWEditText(Context context) {super(context);mthis = this;// TODO Auto-generated constructor stub}@Overridepublic boolean onTouchEvent(MotionEvent e) {if(e.getAction()==MotionEvent.ACTION_DOWN){//通知父控件不要干扰getParent().requestDisallowInterceptTouchEvent(true);}else if(e.getAction()==MotionEvent.ACTION_MOVE){//通知父控件不要干扰getParent().requestDisallowInterceptTouchEvent(true);}else if(e.getAction()==MotionEvent.ACTION_UP){//getParent().requestDisallowInterceptTouchEvent(true);}return super.onTouchEvent(e);}}</span></strong>

第二种方法简单的多了,只需要在你需要使用的时候  用上就可以了

<strong><span style="font-size:18px;"><com.jarvis.views.XWEditText                        android:id="@+id/pub_desc"                        android:layout_width="0dp"                        android:layout_height="match_parent"                        android:layout_weight="3"                        android:background="@null"                        android:gravity="left"                        android:hint="对您想找的工作做简要的描述"                        android:padding="2dp"                        android:textColor="@android:color/black"                        android:textSize="16sp" /></span></strong>
就这样就解决问题了  你在对应的类中用EditText 或者XWEditText都是可以可以找到这个控件,然后不用做任何的设置,就跟EditText那样用就行了,

连接地址:http://download.csdn.net/download/zhaohuofanqie/7684473

0 0
原创粉丝点击