天天看點

android中ScrollView的setOnClickListener無效

近來意欲開發一款立Flag的APP,有一個部分類似便簽,需要一個EditText,而這個EditText外邊又套了一個ScrollView(不用ScrollView的話,内容如果超出螢幕,也可以滑動,不過我需要插入圖檔,如果不用ScrollView的話體驗不好,這裡不詳細贅述),布局如下:

<ScrollView
        android:id="@+id/sv_edit_view"
        android:layout_below="@id/et_edit_title"
        android:layout_above="@id/view_edit_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp">

        <EditText
            android:background="@null"
            android:id="@+id/et_edit_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:textSize="16sp"
            android:textColor="#fff"
            android:hint="請輸入内容"/>
    </ScrollView>      

圖形如下:

android中ScrollView的setOnClickListener無效

很顯然,在沒有内容的時候​

​EditText​

​​隻有一行那麼高,而​

​ScrollView​

​​是​

​match-parent​

​​的,而我自然希望點選​

​ScrollView​

​​的任何一個地方都能讓​

​EditText​

​獲得焦點,是以我寫了如下代碼:

scrollView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("YYPT", "click the scrollView");
                content.setFocusable(true);
                content.setFocusableInTouchMode(true);
                content.requestFocus();
                AddFlagActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        });      

本以為這麼簡單的一件事,随手就寫了,然而點了之後什麼反應都沒有,​

​Logcat​

​裡面也沒有輸出。

scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                //通知父控件請勿攔截本控件touch事件
                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_UP:
                        Log.d("YYPT", "click the scrollView");
                        //點選整個頁面都會讓内容框獲得焦點,且彈出軟鍵盤
                        content.setFocusable(true);
                        content.setFocusableInTouchMode(true);
                        content.requestFocus();
                        AddFlagActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                        break;
                }
                 return false;
            }
        });