在程序UI设计时,我们用遇到,界面内容太多,显示不下,而使用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;
}
});
这样就可以解决我们的问题了。