在我们上传图片和文字的时候,图片总是比较大,以至于界面超出了屏幕显示的范围,这样我们总会在最外层嵌套一个scrollview来保证在超出屏幕显示时能够滑动,但是这样会出现edittext不能滑动的问题,就比如我们在edittext中设置最大line为5,而我们上传的内容大于5行,这样的情况下我们在编辑的时候想修改最开始编辑的内容就必须滑动到开始的时候去修改,然而edittext不能滑动就是一个非常严重的问题。我在网上找到了两种不同的解决办法
一. edittext的内容不管为不为空,在滑动edittext的时候外部的scrollview都不能滑动
editText.setOnTouchListener(new View.OnTouchListener() {
@Overridepublic boolean onTouch(View v, MotionEvent event) {
// 解决scrollView中嵌套EditText导致其不能上下滑动的问题
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
二.edittext为空的时候,外界scrollview可以滑动,不为空scrollview不能滑动
http://blog.youkuaiyun.com/hoyouly/article/details/50986626
1.首先自定义一个线性布局
public class MyLinearLayout extends LinearLayout {
private ScrollView parentScrollview;
private EditText editText;
private int showLineMax = 0;
public void setParentScrollview(ScrollView parentScrollview) {
this.parentScrollview = parentScrollview;
}
public void setEditeText(EditText editText) {
this.editText = editText;
MyLinearLayout.LayoutParams lp = (MyLinearLayout.LayoutParams) editText.getLayoutParams();
showLineMax = lp.height / editText.getLineHeight();
}
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (parentScrollview == null) {
return super.onInterceptTouchEvent(ev);
} else {
if (ev.getAction() == MotionEvent.ACTION_DOWN && editText.getLineCount() >= showLineMax) {
// 将父scrollview的滚动事件拦截
setParentScrollAble(false);
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
// 把滚动事件恢复给父Scrollview
setParentScrollAble(true);
}
}
return super.onInterceptTouchEvent(ev);
}
/**
* 是否把滚动事件交给父scrollview
*
* @param flag
*/
private void setParentScrollAble(boolean flag) {
parentScrollview.requestDisallowInterceptTouchEvent(!flag);
}
}
2.布局文件中将自定义的线性布局嵌套在edittext的外面
<com.kk.view.EditLinearLayout
android:id="@+id/edit_liear"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/de"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="@color/white"
android:gravity="top"
android:hint=".."
android:maxLines="5"
android:padding="10dp"
android:textSize="@dimen/text_size_16" />
</com.kk.view.EditLinearLayout>
3.主函数
public class MainActivity extends Activity {
private ScrollView sv;
private EditText et;
private MyLinearLayout se;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
sv = (ScrollView) findViewById(R.id.sv);
et = (EditText) findViewById(R.id.de);
se = (MyLinearLayout) findViewById(R.id.edit_liear);
se.setParentScrollview(sv);
se.setEditeText(et);
}
}
三.我在想的最好的一种的效果就是,edittext填写内容后,当我们edittext滑动在第一行或者最后一行的时候外层的scrollview可以滑动,我这一找到一个方法但是没有试过,把网址分享给大家看看
http://www.w2bc.com/article/125145