今天項目中需求是寫出一個很簡單的edittext輸入框,但要求當輸入字數過長時需要上下滑動以便查看所有文字,因為頁面底部有一個"確定"的button,但剛開始輸入框內的問題怎么都滑動不了,我一開始就想到了這是事件傳遞沖突問題,但試了很多種方法都不行,最后也是一個一個試才解決的,不多說,貼代碼:
<ScrollView android:id="@+id/sc_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:minHeight="360dp" android:scrollbars="none"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="10dp" android:layout_marginStart="15dp" android:layout_marginTop="10dp" android:background="@null" android:gravity="top|start" android:hint="@string/FeedBackViewController_Placeholder" android:lineSpacingMultiplier="1.0" android:paddingEnd="10dp" android:paddingStart="10dp" android:maxHeight="450dp" //當初這個沒加,也出現了滑動不了的情況 android:textSize="@dimen/font_size16"/> </ScrollView>
代碼里面需要:
editText.setOnTouchListener(new View.OnTouchListener() { @Override public 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; } });以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答