EditText和ScrollView共存,无法获取焦点,不能滚动的解决方法
2014-01-03 15:59
513 查看
在程序UI设计时,我们用遇到,界面内容太多,显示不下,而使用ScrollView进行内容的填充
在使用时,需要注意,ScrollView作为parent时,只能有一个child
当我们使用了ScrollView+EditText后,在输入时发现,当输入文本太多,EditText换行显示,正常情况下是可以滑动内容的,但是现在不行,原因是触摸事件已经被ScrollView消费了,这样我们很容易就想到,对触摸事件进行拦截
这样就可以解决我们的问题了。
在使用时,需要注意,ScrollView作为parent时,只能有一个child
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/top_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/top_img" > <Button android:id="@+id/back_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerInParent="true" android:layout_marginLeft="@dimen/margin_10" android:background="@drawable/back_button" /> <TextView android:id="@+id/title_text" style="@style/top_title_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/my_dynamics" /> <Button android:id="@+id/send_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerInParent="true" android:layout_marginRight="@dimen/margin_10" android:background="@drawable/send_msg_button" /> </RelativeLayout> <EditText android:id="@+id/phone_edit" style="@style/comment_edit_style" android:layout_height="53dp" android:layout_alignParentBottom="true" android:layout_margin="@dimen/margin_10" android:background="@drawable/shape_bg" android:inputType="phone" android:singleLine="true" android:textSize="18sp" /> <TextView android:id="@+id/phone_text" style="@style/black_text_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/phone_edit" android:layout_alignLeft="@id/phone_edit" android:layout_alignRight="@id/phone_edit" android:layout_marginTop="@dimen/margin_10" android:text="@string/phone_no" /> <EditText android:id="@+id/comment_edit" style="@style/comment_edit_style" android:layout_height="match_parent" android:layout_above="@id/phone_text" android:layout_alignLeft="@id/phone_edit" android:layout_alignRight="@id/phone_edit" android:layout_below="@id/top_layout" android:layout_marginTop="@dimen/margin_10" android:background="@drawable/shape_bg" android:focusable="true" android:focusableInTouchMode="true" android:gravity="left" android:hint="@string/app_comment_hint" android:paddingBottom="@dimen/margin_10" android:paddingTop="@dimen/margin_10" android:textSize="18sp" /> </RelativeLayout>
当我们使用了ScrollView+EditText后,在输入时发现,当输入文本太多,EditText换行显示,正常情况下是可以滑动内容的,但是现在不行,原因是触摸事件已经被ScrollView消费了,这样我们很容易就想到,对触摸事件进行拦截
mParentCommentEdit = (EditText) findViewById(R.id.comment_edit); mParentCommentEdit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(120)}); mParentCommentEdit.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub String str = s.toString(); if(str.length() == 120) { Toast.makeText(getApplicationContext(), R.string.text_is_too_long, Toast.LENGTH_LONG).show(); } } }); final ScrollView scorllView = (ScrollView) findViewById(R.id.scroll); mParentCommentEdit.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //重写onTouch()事件,在事件里通过requestDisallowInterceptTouchEvent(boolean) //方法来设置父类的不可用,true表示父类的不可用 if (event.getAction() == MotionEvent.ACTION_UP) { scorllView.requestDisallowInterceptTouchEvent(false); } else { scorllView.requestDisallowInterceptTouchEvent(true); } return false; } });
这样就可以解决我们的问题了。
相关文章推荐
- scrollview 中嵌套 edittext edit无法获取焦点的解决办法
- 当EditText被视图(按钮等)挡住无法获取焦点的解决方法
- ViewPager中嵌套Fragment中的EditText设置requestFocus无法获取焦点的解决方法
- 解决EditText多行编辑时在ScrollView中无法滚动的问题
- Silverlight 4里Textbox在失去焦点之前无法获取到Text值的另类解决方法
- EditText默认无法获取焦点,必须点击一次才能获取焦点打开软键盘的解决办法
- 解决基于SlidingMenu的fragment切换后edittext无法获取焦点的问题
- ListView里面有EditTextView,解决弹出键盘后EditView不能获取焦点的问题
- ListView里面有EditTextView,解决弹出键盘后EditView不能获取焦点的问题
- android EditText获取焦点后与输入框界面冲突解决方法
- ListView里面有EditTextView,解决弹出键盘后EditView不能获取焦点的问题
- 解决ScrollView中嵌套EditText时的滑动滚动冲突
- AndroidTextView的跑马灯效果,解决复杂布局不能获取焦点的问题
- 安卓使用EditText进入一个新页面时自动获取焦点弹出输入法界面解决办法
- 让EditText不能自动获取焦点
- EditText获取焦点的一系列方法
- 关于AlertDialog中EditText不能弹出输入法解决方法
- ListView里面有EditText,弹出键盘后EditView立刻不能获取焦点的问题
- 默认EditTextView不自动获取焦点的方法
- ScrollView无法完全滚动到底部解决方法